dom4j解析简单的xml文件 解析元素并封装到对象
1 package cn.itcast.xml; 2 3 import cn.itcast.domain.Book; 4 import org.dom4j.Document; 5 import org.dom4j.DocumentException; 6 import org.dom4j.Element; 7 import org.dom4j.Node; 8 import org.dom4j.io.SAXReader; 9 import org.junit.Before; 10 import org.junit.Test; 11 12 import java.io.FileNotFoundException; 13 import java.io.InputStream; 14 import java.util.ArrayList; 15 import java.util.List; 16 17 public class Dom4jDemo { 18 19 private Document document; 20 21 @Before 22 public void init() throws DocumentException { 23 SAXReader reader = new SAXReader(); 24 InputStream is = Dom4jDemo.class.getClassLoader().getResourceAsStream("book.xml"); 25 26 document = reader.read(is); 27 } 28 29 @Test 30 public void parseMethod2() throws DocumentException { 31 //获取文档根元素,books 32 Element root = document.getRootElement(); 33 34 //获取所有book元素 35 List<Element> bookElements = root.elements(); 36 37 //解析所有book元素,并添加到books集合中 38 List<Book> books = new ArrayList<>(); 39 40 for (Element bookElement : bookElements) { 41 //获取author属性值 42 String author = bookElement.attributeValue("author"); 43 //获取子元素name的文本 44 String name = bookElement.element("name").getText(); 45 //获取子元素price的文本 46 String priceStr = bookElement.element("price").getText(); 47 double price = Double.parseDouble(priceStr); 48 //创建对应的book对象,并添加到books集合中 49 books.add(new Book(author, name, price)); 50 } 51 52 //打印 53 books.forEach(System.out::println); 54 55 } 56 57 @Test 58 public void parseMethod1() throws FileNotFoundException, DocumentException { 59 //获取根元素 60 Element root = document.getRootElement(); 61 System.out.println(root.getName()); 62 63 List<Book> books = new ArrayList<>(); 64 65 //获取所有book元素,解析并保存到books集合中。 66 List<Node> bookNodes = document.selectNodes("/books/book"); 67 for (Node bookNode : bookNodes) { 68 //获取作者 69 String author = bookNode.valueOf("@author"); 70 // System.out.println(author); 71 72 /* 73 斜杠开头表示文档,中间的斜杠则表示直接子节点 74 错误代码: 75 String name = bookNode.valueOf("/name"); 76 表示选区文档下的name元素,而文档下只有books元素。 77 */ 78 //获取书名 79 List<Node> nameNodes = bookNode.selectNodes("name"); 80 String bookName = nameNodes.get(0).getText(); 81 // System.out.println(bookName); 82 83 //获取价格 84 List<Node> priceNodes = bookNode.selectNodes("price"); 85 double price = (double) Double.parseDouble(priceNodes.get(0).getText()); 86 87 books.add(new Book(author, bookName, price)); 88 } 89 90 //打印结果 91 books.forEach(System.out::println); 92 } 93 }
1 package cn.itcast.domain; 2 3 public class Book { 4 private String author; 5 private String name; 6 private Double price; 7 8 public Book(){} 9 10 public Book(String author, String name, Double price) { 11 this.author = author; 12 this.name = name; 13 this.price = price; 14 } 15 16 public String getAuthor() { 17 return author; 18 } 19 20 public void setAuthor(String author) { 21 this.author = author; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 public Double getPrice() { 33 return price; 34 } 35 36 public void setPrice(Double price) { 37 this.price = price; 38 } 39 40 @Override 41 public String toString() { 42 return "Book{" + 43 "author='" + author + '\'' + 44 ", name='" + name + '\'' + 45 ", price=" + price + 46 '}'; 47 } 48 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <books> 3 <book author="天蚕土豆"> 4 <name>斗破苍穹</name> 5 <price>86</price> 6 </book> 7 <book author="萧潜"> 8 <name>缥缈之旅</name> 9 <price>92</price> 10 </book> 11 <book author="萧鼎"> 12 <name>诛仙</name> 13 <price>98</price> 14 </book> 15 <book author="天下霸唱"> 16 <name>鬼吹灯</name> 17 <price>124</price> 18 </book> 19 <book author="辰东"> 20 <name>神墓</name> 21 <price>128</price> 22 </book> 23 </books>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>cn.itcast.xml-parser</groupId> 8 <artifactId>xml-parser-demo1</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <dependencies> 12 <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j --> 13 <dependency> 14 <groupId>org.dom4j</groupId> 15 <artifactId>dom4j</artifactId> 16 <version>2.1.1</version> 17 </dependency> 18 19 <!-- https://mvnrepository.com/artifact/jaxen/jaxen --> 20 <dependency> 21 <groupId>jaxen</groupId> 22 <artifactId>jaxen</artifactId> 23 <version>1.1.6</version> 24 </dependency> 25 26 27 <dependency> 28 <groupId>junit</groupId> 29 <artifactId>junit</artifactId> 30 <version>4.12</version> 31 </dependency> 32 </dependencies> 33 34 </project>
1 package cn.itcast.xml; 2 3 import org.dom4j.Document; 4 import org.dom4j.Element; 5 import org.dom4j.io.SAXReader; 6 import org.junit.Test; 7 8 import java.util.List; 9 /* 10 作者:北冥有鱼_很好吃 11 链接:https://www.jianshu.com/p/806bb1bdc06f 12 */ 13 public class Dom4j_02{ 14 15 @Test 16 public void parse() throws Exception { 17 // 1. 创建一个 saxReader 对象 18 SAXReader saxReader = new SAXReader(); 19 // 2. 将数据读取到 document 对象中 20 Document document = saxReader.read(Dom4j_02.class.getClassLoader().getResourceAsStream("servlet.xml")); 21 // 3. 使用 document 对象调用 getRootElement 方法获取根标签, 返回 Element 接口实现类对象 22 Element rootElement = document.getRootElement(); 23 // 4. 使用 rootElement 根标签对象调用 elements 方法, 传入 servlet, 获取servlet标签对象 24 List<Element> servletElements = rootElement.elements("servlet"); 25 // 5. 遍历, 并获取该标签下的子标签数据内容, 使用父标签对象调用elementText方法, 传入子标签名称获取数据 26 for (Element servlet : servletElements) { 27 String name = servlet.elementText("servlet-name"); 28 String cls = servlet.elementText("servlet-class"); 29 System.out.println(name + " : " + cls); 30 } 31 // 使用 rootElement 根标签对象调用 elements 方法, 传入 servlet-mapping, 获取servlet-mapping标签对象 32 List<Element> mappingElements = rootElement.elements("servlet-mapping"); 33 for (Element mapping : mappingElements) { 34 String name = mapping.elementText("servlet-name"); 35 String url = mapping.elementText("url-pattern"); 36 System.out.println(name + " = " + url); 37 } 38 } 39 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 3 作者:北冥有鱼_很好吃 4 链接:https://www.jianshu.com/p/806bb1bdc06f 5 --> 6 <servlets> 7 <servlet> 8 <servlet-name>servlet1</servlet-name> 9 <servlet-class>MyServlet1</servlet-class> 10 </servlet> 11 <servlet> 12 <servlet-name>servlet2</servlet-name> 13 <servlet-class>MyServlet2</servlet-class> 14 </servlet> 15 <servlet-mapping> 16 <servlet-name>servlet1</servlet-name> 17 <url-pattern>/s1</url-pattern> 18 </servlet-mapping> 19 <servlet-mapping> 20 <servlet-name>servlet2</servlet-name> 21 <url-pattern>/s2</url-pattern> 22 </servlet-mapping> 23 </servlets>