第5 章 : XML解析之dom4j
课时31 dom4j的简介
依赖
<dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.0.0</version> </dependency>
demo.xml
<?xml version="1.0" encoding="UTF-8"?> <list> <person> <name>张三</name> <age>23</age> </person> <person> <name>李四</name> <age>24</age> </person> </list>
课时32 使用dom4j实现查询xml操作
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.util.List; class Demo { public static void main(String[] args) throws DocumentException { // 创建解析器 SAXReader reader = new SAXReader(); // 得到Document Document document = reader.read("demo.xml"); // 获取根元素 Element root = document.getRootElement(); // 得到所有person标签 List<Element> list = root.elements("person"); // 遍历标签 for (Element element : list) { Element name = element.element("name"); // 获取值 String text = name.getText(); System.out.println(text); } } }
课时33 使用dom4j实现在末尾添加节点的操作
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import java.io.FileOutputStream; import java.io.IOException; class Demo { public static void main(String[] args) throws DocumentException, IOException { // 创建解析器, 得到Document SAXReader reader = new SAXReader(); Document document = reader.read("demo.xml"); // 获取根元素 Element root = document.getRootElement(); // 添加元素并设置内容 Element person = root.element("person"); Element sex = person.addElement("sex"); sex.setText("男"); // 回写并格式化 OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("demo.xml"), format); xmlWriter.write(document); xmlWriter.close(); } }
课时34 使用dom4j实现在在特定位置添加节点的操作
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; class Demo { public static void main(String[] args) throws DocumentException, IOException { // 创建解析器, 得到Document SAXReader reader = new SAXReader(); Document document = reader.read("demo.xml"); Element root = document.getRootElement(); // 指定位置添加元素 Element person = root.element("person"); List<Element> list = person.elements(); Element sex = DocumentHelper.createElement("sex"); sex.setText("女"); list.add(1, sex); // 回写并格式化 OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("demo.xml"), format); xmlWriter.write(document); xmlWriter.close(); } }
课时35 dom4j里面封装方法的操作
package util; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import java.io.FileOutputStream; import java.io.IOException; public class Dom4jUtil { public static Document getDocument(String path) throws DocumentException { // 创建解析器, 得到Document SAXReader reader = new SAXReader(); return reader.read(path); } public static void writeXml(String path, Document document) throws IOException { // 回写并格式化 OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path), format); xmlWriter.write(document); xmlWriter.close(); } }
课时36 使用dom4j实现修改的操作
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import util.Dom4jUtil; import java.io.IOException; class Demo { public static void main(String[] args) throws DocumentException, IOException { // 创建解析器, 得到Document String path = "demo.xml"; Document document = Dom4jUtil.getDocument(path); // 获取根元素 Element root = document.getRootElement(); Element peron = root.element("person"); Element sex = peron.element("sex"); sex.setText("男"); // 回写并格式化 Dom4jUtil.writeXml(path, document); } }
课时37 使用dom4j实现删除节点的操作
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import util.Dom4jUtil; import java.io.IOException; class Demo { public static void main(String[] args) throws DocumentException, IOException { String path = "demo.xml"; Document document = Dom4jUtil.getDocument(path); // 获取根元素 Element root = document.getRootElement(); Element person = root.element("person"); Element sex = person.element("sex"); // 通过父节点删除 sex.getParent().remove(sex); // 回写并格式化 Dom4jUtil.writeXml(path, document); } }
课时38 使用dom4j实现获取属性值的操作
demo.xml <?xml version="1.0" encoding="UTF-8"?> <person id="001"> <name>张三</name> <age>23</age> </person>
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import util.Dom4jUtil; import java.io.IOException; class Demo { public static void main(String[] args) throws DocumentException, IOException { String path = "demo.xml"; Document document = Dom4jUtil.getDocument(path); Element root = document.getRootElement(); String id = root.attributeValue("id"); System.out.println(id); // 001 } }
第6 章 : XPATH
课时39 XPATH简介
dom4j支持xpath操作
可以直接获取元素
/a/b/c 一层一层选择 //b 不管层级,直接选择b /* 所有元素 /a/b[1] 第一个b元素 /a/b[last()] 最后一个b元素 //b[@id] 只要有id属性 //b[@name="bbb"] name属性等于bbb
课时40 使用dom4j支持XPATH的操作一
jaxen selectNode() selectSingleNode() demo.xml <?xml version="1.0" encoding="UTF-8"?> <person id="001"> <name>张三</name> <age>23</age> </person>
示例:得到所有name节点
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Node; import util.Dom4jUtil; import java.util.List; class Demo { public static void main(String[] args) throws DocumentException { String path = "demo.xml"; Document document = Dom4jUtil.getDocument(path); // 获取元素 List<Node> list = document.selectNodes("//name"); // 遍历集合 for (Node node : list) { String text = node.getText(); System.out.println(text); } } }
课时41 使用dom4j支持XPATH的操作二
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Node; import util.Dom4jUtil; class Demo { public static void main(String[] args) throws DocumentException { String path = "demo.xml"; Document document = Dom4jUtil.getDocument(path); // 获取根元素 Node name = document.selectSingleNode("/person[@id='001']/name"); System.out.println(name.getText()); } }
第7 章 : 案例
课时42-44 学生管理系统实现-添加、删除、查询操作
使用xml作为数据库,存储学生数据
依赖
<dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.0.0</version> </dependency>
demo.xml
<?xml version="1.0" encoding="UTF-8"?> <list> </list>
Dom4jUtil.java
package util;
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import java.io.FileOutputStream; import java.io.IOException; public class Dom4jUtil { public static Document getDocument(String path) throws DocumentException { // 创建解析器, 得到Document SAXReader reader = new SAXReader(); return reader.read(path); } public static void writeXml(String path, Document document) throws IOException { // 回写并格式化 OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path), format); xmlWriter.write(document); xmlWriter.close(); } }
Student.java
package com.pengshiyu.student; public class Student { private String id; private String name; private int age; public Student() { } public Student(String id, String name, int age) { this.id = id; this.name = name; this.age = 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age=" + age + '}'; } }
StudentService.java
package com.pengshiyu.student; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import util.Dom4jUtil; import java.io.IOException; import java.util.List; public class StudentService { private static final String path = "demo.xml"; /** * 添加数据 * * @param student * @throws DocumentException * @throws IOException */ public static void addStudent(Student student) throws DocumentException, IOException { Document document = Dom4jUtil.getDocument(path); Element root = document.getRootElement(); Element person = root.addElement("person"); Element id = person.addElement("id"); id.setText(student.getId()); Element name = person.addElement("name"); name.setText(student.getName()); Element age = person.addElement("age"); age.setText(String.valueOf(student.getAge())); Dom4jUtil.writeXml(path, document); } /** * 删除数据 * * @param uid * @throws IOException * @throws DocumentException */ public static void removeStudent(String uid) throws IOException, DocumentException { Document document = Dom4jUtil.getDocument(path); // 获取所有的id List<Node> list = document.selectNodes("//id"); for (Node node : list) { String nodeId = node.getText(); // System.out.println(nodeId); // 判断id相同,通过祖父节点移除父节点 if (uid.equals(nodeId)) { Element parent = node.getParent(); Element grandfather = parent.getParent(); grandfather.remove(parent); } } Dom4jUtil.writeXml(path, document); } /** * 查询数据 * * @param uid * @return * @throws IOException * @throws DocumentException */ public static Student getStudent(String uid) throws DocumentException { Document document = Dom4jUtil.getDocument(path); // 获取所有的id List<Node> list = document.selectNodes("//id"); for (Node node : list) { String nodeId = node.getText(); // System.out.println(nodeId); // 判断id相同,通过祖父节点移除父节点 if (uid.equals(nodeId)) { Element parent = node.getParent(); Student student = new Student(); student.setId(parent.element("id").getText()); student.setName(parent.element("name").getText()); student.setAge(Integer.parseInt(parent.element("age").getText())); return student; } } return null; } }
StudentTest.java
package com.pengshiyu.student; import org.dom4j.DocumentException; import org.junit.Test; import java.io.IOException; public class StudentTest { @Test public void testAddStudent() throws IOException, DocumentException { Student student = new Student("001", "Tom", 23); StudentService.addStudent(student); } @Test public void testRemoveStudent() throws IOException, DocumentException { StudentService.removeStudent("001"); } @Test public void testGEtStudent() throws IOException, DocumentException { Student student= StudentService.getStudent("001"); System.out.println(student); } }