<?xml version="1.0" encoding="UTF-8"?>
<student>
<stu>
<name>张三</name>
<age>16</age>
<sex>男</sex>
</stu>
<stu>
<name>李四</name>
<age>18</age>
<sex>男</sex>
</stu>
<stu>
<name>王五</name>
<age>17</age>
<sex>男</sex>
</stu>
</student>
package cn.itcast.vo;
public class Student {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String toString(){
return name+":"+age+":"+sex;
}
}
package student;
import java.io.FileOutputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import cn.itcast.vo.Student;
public class StuServer {
//增加
public static void addStu(Student s) throws Exception{
SAXReader sax = new SAXReader();
Document document = sax.read("src/student.xml");
Element root = document.getRootElement();
Element stu = root.addElement("stu");
Element name = stu.addElement("name");
Element age = stu.addElement("age");
Element sex = stu.addElement("sex");
name.setText(s.getName());
age.setText(s.getAge()+"");
sex.setText(s.getSex());
//回写
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlwriter = new XMLWriter(new FileOutputStream("src/student.xml"),format);
xmlwriter.write(document);
xmlwriter.close();
}
//删除 根据学生的id
public static void delStu(String name)throws Exception{
SAXReader sax = new SAXReader();
Document document = sax.read("src/student.xml");
List<Node> list=document.selectNodes("//name");
for(Node node:list){
String namev = node.getText();
if(namev.equals(name)){
Element stu = node.getParent();
Element student = stu.getParent();
student.remove(stu);
}
}
//回写
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlwriter = new XMLWriter(new FileOutputStream("src/student.xml"),format);
xmlwriter.write(document);
xmlwriter.close();
}
public static Student selectStu(String name)throws Exception{
SAXReader sax = new SAXReader();
Document document = sax.read("src/student.xml");
List<Node>list = document.selectNodes("//name");
Student s = new Student();
for(Node node : list){
String namev = node.getText();
if(namev.equals(name)){
Element stu = node.getParent();
int agev =Integer.parseInt(stu.element("age").getText());
String sexv = stu.element("sex").getText();
s.setAge(agev);
s.setName(namev);
s.setSex(sexv);
}
}
return s;
}
}
package cn.itcast.test;
import cn.itcast.vo.Student;
import student.StuServer;
public class Test {
public static void main(String[] args) throws Exception{
// addTest();
// delTest();
selectTest();
}
public static void addTest() throws Exception{
Student s = new Student();
s.setName("tom");
s.setAge(20);
s.setSex("男");
StuServer.addStu(s);
}
public static void delTest()throws Exception{
StuServer.delStu("tom");
}
public static void selectTest()throws Exception{
Student student = StuServer.selectStu("王五");
System.out.println(student.toString());
}
}