环境:
myeclipse 10
window 10
导入包:dom4j-1.6.1.jar jaxen-1.1-beta-6.jar
代码实现:
<?xml version="1.0" encoding="UTF-8"?> <student> <stu> <id>100</id> <name>张三</name> <age>20</age> </stu> <stu> <id>101</id> <name>李四</name> <age>30</age> </stu> </student>
package com.java.xml; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; 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; public class StudentService { /** * 增加 添加学生信息 * @param student * @throws DocumentException * @throws IOException */ public static void addStudent(Student student) throws DocumentException,IOException { /* * 1.创建解析器 * 2.得到document * 3.获取根节点 * 4.在根节点上面创建stu标签 * 5.在stu标签上面创建id name age * 6.分别给id name age元素赋值 * 7.回写xml */ //创建解析器 SAXReader saxReader = new SAXReader(); //得到Document Document document = saxReader.read("src/student.xml"); //获取根节点 Element root = document.getRootElement(); //在根节点上面添加stu标签 Element stu = root.addElement("stu"); //在stu上面添加id name age Element id = stu.addElement("id"); Element name = stu.addElement("name"); Element age = stu.addElement("age"); //分别给id name age元素赋值 id.setText(student.getId()); name.setText(student.getName()); age.setText(String.valueOf(student.getAge())); //回写xml OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"),format); xmlWriter.write(document); xmlWriter.close(); } /** * 删除 根据id删除学生 * @param id * @throws DocumentException * @throws IOException */ public static void deleteStudent(String id) throws DocumentException,IOException { /* * 1.创建解析器 * 2.得到document * 3.获取所有的id,使用xpath //id 返回 list集合 * 4.遍历list集合 * 5.判断集合里面的id和传递的id是否相同 * 6.如果相同把id所在的stu删除 * 7.回写xml */ //创建解析器 SAXReader saxReader = new SAXReader(); //得到document Document document = saxReader.read("src/student.xml"); //获取所有的id List<Node> list = document.selectNodes("//id"); //遍历list集合 for (Node node : list) { //node是每一个id的元素,得到id的值 String value = node.getText(); //判断value与传递的id是否相同 if(value.equals(id)) { //id相同,得到stu标签 Element stu = node.getParent(); //获取stu的父节点 Element student = stu.getParent(); //删除stu student.remove(stu); } } //回写xml OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"),format); xmlWriter.write(document); xmlWriter.close(); } /** * 查询 根据id查询学生信息 * @param id * @throws DocumentException */ public static Student getStudent(String id) throws DocumentException { /* * 1.创建解析器 * 2.得到document * 3.获取所有的id,返回list集合 * 4.遍历list集合 * 5.得到每一个id的节点 * 6.得到id的节点值 * 7.判断id的值与传递的值是否相同 * 8.如果相同,先获取到父节点stu * 9.通过stu获取到name,age */ //创建解析器 SAXReader saxReader = new SAXReader(); //得到document Document document = saxReader.read("src/student.xml"); //获取所有的id,得到集合list List<Node> list = document.selectNodes("//id"); //创建Student对象 Student student = new Student(); //遍历集合list for (Node node : list) { //node是每一个id节点,得到id节点的值 String value = node.getText(); //判读值是否与传入的相同 if(value.equals(id)) { //得到id的父节点 Element stu = node.getParent(); //通过stu获取name,age String name = stu.element("name").getText(); String age = stu.element("age").getText(); student.setId(value); student.setName(name); student.setAge(Integer.parseInt(age)); } } return student; } }
package com.java.xml; import java.io.IOException; import org.dom4j.DocumentException; public class StudentCRUD { public static void main(String[] args) throws DocumentException,IOException { // addStudent(); // deleteStudent("102"); getStudent("101"); } public static void addStudent() throws DocumentException,IOException { Student student = new Student(); student.setId("102"); student.setName("王五"); student.setAge(35); StudentService.addStudent(student); } public static void deleteStudent(String id) throws DocumentException,IOException { StudentService.deleteStudent(id); } public static void getStudent(String id) throws DocumentException { Student student = StudentService.getStudent(id); System.out.println(student); } }