A, Dom4j Technical Overview
First Jdom is encapsulated in dom basis and also on dom4j jdom is encapsulated.
DOM4J is an open source, Java-based library to parse XML documents, it has a high degree of flexibility, high performance and memory efficiency of the API. This is the java optimization, using Java as a collection of lists and arrays. It can use the DOM, SAX, XPath and XSLT. It has a very low memory footprint when parsing large XML documents.
Two, Dom4j library use
Go to the official website to download the required jar package: Dom4J official website
Three, DOM4j analysis step
step:
1, the first load xml file created Document object
2, to get the object through the root element Document object
3, by the root element .elelemts ( tag name ); returns a collection, the collection stood. All elements of the object you specify the name of the label
4, to obtain elements need to operate, the appropriate action
XML file:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <books>
3 <book sn="123">
4 <name>Java</name>
5 <price>9.9</price>
6 <author>老张</author>
7 </book>
8 <book sn="456">
9 <name>Python</name>
10 <price>99.99</price>
11 <author>老李</author>
12 </book>
13 </books>
Test parsing file:
. 1 / *
2 * Get the dom4j Documet objects
. 3 * /
. 4 @Test
. 5 public void the getDocument () throws DocumentException {
. 6 // To create a Document object, we need to create a SAXReader objects
. 7 SAXReader Reader = new new SAXReader ();
. 8 // this object is used to read the xml file, and then returns a document.
9 the Document = reader.Read the Document ( "src / the Books.xml" );
10 // print to the console to see whether to create a successful
11 System.out.println (the Document);
12 }
Parsing XML file:
1 /*
2 * 读取 xml 文件中的内容
3 */
4 @Test
5 public void readXML() throws DocumentException {
6 // 需要分四步操作:
7 // 第一步, 通过创建 SAXReader 对象。 来读取 xml 文件, 获取 Document 对象
8 // 第二步, 通过 Document 对象。 拿到 XML 的根元素对象
9 // 第三步, 通过根元素对象。 获取所有的 book 标签对象
10 // 第四步, 遍历每个 book 标签对象。 然后获取到 book 标签对象内的每一个元素, 再通过 getText() 方法拿到起始标签和结束标签之间的文本内容
11 // 第一步, 通过创建 SAXReader 对象。 来读取 xml 文件, 获取 Document 对象
12 SAXReader reader = new SAXReader();
13 Document document = reader.read("src/books.xml");
14 // 第二步, 通过 Document 对象。 拿到 XML 的根元素对象
15 Element root = document.getRootElement();
16 // 打印测试
17 // Element.asXML() 它将当前元素转换成为 String 对象
18 // System.out.println( root.asXML() );
19 // 第三步, 通过根元素对象。 获取所有的 book 标签对象
20 // Element.elements(标签名)它可以拿到当前元素下的指定的子元素的集合
21 List<Element> books = root.elements("book");
22 // 第四步, 遍历每个 book 标签对象。 然后获取到 book 标签对象内的每一个元素,
23 for (Element book : books) {
24 // 测试
25 // System.out.println(book.asXML());
26 // 拿到 book 下面的 name 元素对象
27 Element nameElement = book.element("name");
28 // 拿到 book 下面的 price 元素对象
29 Element priceElement = book.element("price");
30 // 拿到 book 下面的 author 元素对象
31 Element authorElement = book.element("author");
32 // 再通过 getText() 方法拿到起始标签和结束标签之间的文本内容
33 System.out.println("书名" + nameElement.getText() + " , 价格:"
34 + priceElement.getText() + ", 作者: " + authorElement.getText());
35 }
36 }