XML parsing of java (DOM4J Technology)

Parsing XML file with DOM4J Technology

I. Introduction to XML

xml (Extensible Markup Language)
Extensible meaning: allows programmers to extend new tags according to their own ideas
Note: but when extending, you must follow the XML specification

Second, the characteristics of xml:
Self description,
It can support cross platform,
Retains the hierarchy of object-oriented programming


3. XML file specification example

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <persons>
 3 <person id="101">
 4    <name>Li Bai</name>
 5    <addrss>The Tang Dynasty-Changan</addrss>
 6 </person>
 7 <person id="102">
 8    <name>Du Fu</name>
 9    <addrss>The Tang Dynasty-Changan</addrss></person>
10 <person id="103">
11    <name>Su Shi</name>
12    <addrss>Song dynasty-Bian Liang</addrss> 
13 </person>
14 </persons>


IV. details of DOM4J Technology

Dom4j: Dom4j is an easy-to-use, open source library for XML, XPath, and XSLT. It applies to Java platform, adopts Java collection framework and fully supports DOM, SAX and JAXP. In the java development community, Dom4j is the most widely used. The operation of JDOM is very simple, but it does not perform well when processing large XML files, and memory overflow occurs when parsing 10M documents. Dom4j is a very excellent Java XML API, which has the characteristics of excellent performance, powerful function and easy to use. At the same time, it is also an open source software. Dom4j uses a lot of interfaces, which is the reason why it is considered to be more flexible than JDOM. Now we can see that more and more Java software is using Dom4j to read and write XML. The official address of Dom4j is "http://www.dom4j.org /".
Note: DOM4J is not sun's own product, so there is no jar package to provide services. We can download the package ourselves, including API (index HTML), jar package, etc.


V. DOM4J technology parses the detailed code of the above XML file

 1 import java.util.Iterator;
 2 import java.util.List;
 3 import org.dom4j.Attribute;
 4 import org.dom4j.Document;
 5 import org.dom4j.Element;
 6 import org.dom4j.io.SAXReader;
 8 public class dome4j {
 9     public static void main(String[] args) throws Exception {
10         //1.Create is event resolver
11         SAXReader sr = new SAXReader();
12         //2.read XML File and create a text container
13         Document doc = sr.read("Dom4j.xml");
14         //3.Obtain XML Root node
15         Element el= doc.getRootElement();
16         //4.Traverse root node
17         Iterator<Element> it = el.elementIterator();
18         while(it.hasNext()){
19             Element el2 = it.next();
20             //5.Get the attribute value of the root node
21             List<Attribute> list = el2.attributes();
22             for (Attribute a : list) {
23                 String name = a.getName();
24                 String text = a.getText();
25                 System.out.println("=====================");
26                 System.out.println(name+"="+text);
27                 //6.Get by traversal id Corresponding property value
28                 Iterator<Element> it2 = el2.elementIterator();
29                 while(it2.hasNext()){
30                     Element next = it2.next();
31                     String name2 = next.getName();
32                     String text2 = next.getText();
33                     System.out.println(name2+"="+text2);
34                 }
35             }
36         }
37     }
38 }


Excellent java learning video

java multithreading technology: link: https://pan.baidu.com/s/1vsoerp2a4sxd4tu1numeg extraction code: qxfe

Keywords: Java xml Attribute Programming

Added by e39m5 on Sun, 01 Dec 2019 03:14:51 +0200