数据信息层:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="1">
<name>soso</name>
<password>123</password>
<age>10</age>
</student>
</students>
javabean:
package cn.itcast.xml.parser.example2;
public class Student {
private int id;
private String name;
private String password;
private int age;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
DAO:
package cn.itcast.xml.parser.example2;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
//数据访问层(对XML文件的CURD操作)
public class StudentDao {
private Document getDocument() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/cn/itcast/xml/parser/example2/students.xml"));
return document;
}
private void writerToFile(Document document)throws Exception{
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
Writer writer = new OutputStreamWriter(new FileOutputStream("src/cn/itcast/xml/parser/example2/students.xml"),"UTF-8");
XMLWriter xmlWriter = new XMLWriter(writer,format);
xmlWriter.write(document);
xmlWriter.close();
}
public void create(Student student) throws Exception{
/*
Document document = this.getDocument();
Element rootElement = document.getRootElement();//<students>根元素和document已绑定在一起
Element studentElement = rootElement.addElement("student");//<student>
studentElement.addElement("name").setText(student.getName());//<name>jack</name>
studentElement.addElement("password").setText(student.getPassword());//<password>jack</password>
studentElement.addElement("age").setText(student.getAge()+"");//<age>jack</age>
studentElement.addAttribute("id",student.getId()+"");
this.writerToFile(document);
*/
//必须保存在第一次填加学员时执行,第N次不执行
Document document = DocumentHelper.createDocument();
Element rootElement = DocumentHelper.createElement("students");
//思路:第N次判段是否已有根元素,如果有,不需要再创建根元素,直接将子元素加到<students>里面
Element studentElement = rootElement.addElement("student");//<student>
studentElement.addElement("name").setText(student.getName());//<name>jack</name>
studentElement.addElement("password").setText(student.getPassword());//<password>jack</password>
studentElement.addElement("age").setText(student.getAge()+"");//<age>jack</age>
studentElement.addAttribute("id",student.getId()+"");
document.add(rootElement);
this.writerToFile(document);
}
public void delete(String id) throws Exception{
Document document = this.getDocument();
String xpath = "//student[@id='"+id+"']";
Element element = (Element) document.selectSingleNode(xpath);
if(element!=null){
element.getParent().remove(element);
this.writerToFile(document);
}else{
throw new RuntimeException("查无此学员");
}
}
}
UI:
package cn.itcast.xml.parser.example2;
import java.util.Scanner;
public class StudentUI {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
/*
System.out.print("需要删除学员的编号:");
String id = scanner.nextLine();
StudentDao studentDao = new StudentDao();
try {
studentDao.delete(id);
System.out.println("删除成功");
} catch (Exception e) {
e.printStackTrace();
}
*/
System.out.print("姓名:");
String name = scanner.nextLine();
System.out.print("密码:");
String password = scanner.nextLine();
System.out.print("年龄:");
String age = scanner.nextLine();
Student student = new Student();
student.setId(Util.getId());
student.setName(name);
student.setPassword(password);
student.setAge(Integer.parseInt(age));
StudentDao studentDao = new StudentDao();
try {
studentDao.create(student);
System.out.println("成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}