<?xml version="1.0" encoding="UTF-8"?>
<student>
<stu>
<id>100</id>
<name>zhangsan</name>
<age>20</age>
</stu>
<stu>
<id>101</id>
<name>wangwu</name>
<age>40</age>
</stu>
</student>
package vo_;
public class Student {
private String id;
private String name;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
package service;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import vo_.Student;
import java.io.FileOutputStream;
import java.util.List;
public class StuService {
//增加
public static void add(Student student) throws Exception {
//创建解析器
SAXReader saxReader = new SAXReader();
//得到document
Document document = saxReader.read("src/student.xml");
//得到根节点
Element rootElement = document.getRootElement();
//在根节点上添加stu
Element stu = rootElement.addElement("stu");
//在stu上添加id name age 标签
Element id1 = stu.addElement("id");
Element name1 = stu.addElement("name");
Element age1 = stu.addElement("age");
//在ID name age 上添加值
id1.setText(student.getId());
name1.setText(student.getName());
age1.setText(student.getAge());
//回写xml
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 id) throws Exception {
//创建解析器
SAXReader saxReader = new SAXReader();
//得到document
Document document = saxReader.read("src/student.xml");
//获取所有的id xpath://id
List<Node> list = document.selectNodes("//id");
//遍历list集合
for (Node node :
list) {//node是每个id的元素
//得到id的值
String idv = node.getText();
//判断idv和id是否相等
if (idv.equals(id)) {
//id相同
Element stu = node.getParent();
//获取stu的父节点
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();
}
//查询,根据id查询学生信息
public static Student getStu(String id) throws Exception {
//创建一个student对象
Student student = new Student();
//创建解析器
SAXReader saxReader = new SAXReader();
//得到document
Document document = saxReader.read("src/student.xml");
//获取所有的id xpath://id
List<Node> list = document.selectNodes("//id");
for (Node node : list) {//node是每个id的节点
//得到id的值
String idv = node.getText();
if (idv.equals(id)) {
Element stu = node.getParent();
//通过stu获得age和name
String namev = stu.element("name").getText();
String agev = stu.element("age").getText();
student.setAge(agev);
student.setName(namev);
student.setId(idv);
}
}
return student;
}
}
package test;
import service.StuService;
import vo_.Student;
public class TestStu {
public static void main(String[] args) throws Exception {
testAdd();
// testDel();
// testStr();
}
//测试添加方法
public static void testAdd() throws Exception {
Student stu = new Student();
stu.setAge("103");
stu.setName("wangwu");
stu.setAge("40");
StuService.add(stu);
}
//测试删除方法
public static void testDel() throws Exception {
StuService.delStu("101");
}
//测试查询方法
public static void testStr() throws Exception {//查询id为100的信息
Student stu = StuService.getStu("100");
System.out.println(stu.toString());
}
}