


Detailed introduction to the sample code of four methods of parsing Xml
Keywords: Java parsing xml, four methods of parsing xml, DOM, SAX, JDOM, DOM4j, XPath
[Introduction]
Technology currently used to parse XML in Java There are many, the mainstream ones include DOM, SAX, JDOM, and DOM4j. The following mainly introduces the use, advantages, disadvantages, and performance tests of these four XML document parsing technologies.
1. [Basic Knowledge - Literacy]
sax and dom are two methods for parsing xml documents (no specific implementation, just interfaces), so they alone cannot parse xml Document; jaxp is just an API, which further encapsulates the sax and dom interfaces, and provides DomcumentBuilderFactory/DomcumentBuilder and SAXParserFactory/SAXParser (the xerces interpreter is used by default).
2. [Simple introduction to using DOM, SAX, JDOM, and DOM4j]
1. [DOM (Document Object Model)]
By W3C Provides an interface that reads the entire XML document into memory and builds a DOM tree to operate each node (Node).
Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
text.xml is used in the following code (the document is placed in the src path, and is compiled in the classes path), which refers to the xml document.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
2. [SAX (Simple API for XML)]SAX does not need to load the entire document into memory, it is based on
eventdriver API (Observer mode), users only need to register the events they are interested in. SAX provides EntityResolver, DTDHandler, ContentHandler, and ErrorHandler interfaces, which are used to monitor parsing entity events, DTD processing events, text processing events, and processing error events respectively, similar to AWT. SAX also provides a default class DefaultHandler for these four interfaces (the default implementation here is actually an empty method). Generally, you only need to inherit DefaultHandler and rewrite the events you are interested in. Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
3. [JDOM]JDOM is very similar to DOM. It is a pure JAVA API that processes XML. The API uses the Collections class extensively, and JDOM only uses concrete classes and not interfaces. JDOM itself does not contain a parser. It typically uses a SAX2 parser to parse and validate input XML documents (although it can also take previously constructed DOM representations as input). It contains converters to output JDOM representations into SAX2 event streams, DOM
Models, or XML text documentsSample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
4, [DOM4j]dom4j is currently the best in xml parsing (Hibernate and Sun's JAXM also use dom4j to parse XML). It incorporates many functions beyond basic XML document representation, including integrated XPath support, XML Schema Support and event-based processing for large documents or streaming documents
Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
Running parameters: -Xms400m -Xmx400m
xml file size: 10.7M
Result:
DOM: >581297ms
SAX: 8829ms
JDOM: 581297ms
DOM4j: 5309ms
The time includes IO, just a simple test,
for reference only! ! ! !
1. [DOM] DOM is a tree-based structure, which usually requires loading the entire document and constructing the DOM tree , and then you can start working.
Advantages:
a. Since the entire tree is in memory, the xml document can be randomly accessed
b. The xml document can be modified
c. DOM is easier to use than SAX .
Disadvantages:
a. The entire document must be parsed at one time
a. Since the entire document needs to be loaded into memory, the cost is high for large documents
2. [SAX] SAX is similar to streaming media. It is event-driven, so there is no need to load the entire document into memory. Users only need to listen to the events they are interested in.
Advantages:
a. There is no need to load the entire xml document into memory, so it consumes less memory.
b. Multiple ContentHandlers can be registered.
Disadvantages:
a. Unable to randomly access the contents of xml Node
b. The document cannot be modified
3. [JDOM]JDOM is a pure Java API for processing XML. The Collections class is widely used in its API.
Advantages:
a. Advantages of DOM method
b. Java rules with SAX
Disadvantages
a. Disadvantages of DOM method
4. [DOM4J]These 4 Among all the XML parsing methods, it is the best one, combining ease of use and performance.
五、【小插曲XPath】
XPath 是一门在 XML 文档中查找信息的语言, 可用来在 XML 文档中对元素和属性进行遍历。XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointer 同时被构建于 XPath 表达之上。因此,对 XPath 的理解是很多高级 XML 应用的基础。
XPath非常类似对数据库操作的SQL语言,或者说JQuery,它可以方便开发者抓起文档中需要的东西。(dom4j也支持xpath)
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
六、【补充】
注意4种解析方法对TextNode(文本节点)的处理:
1、在使用DOM时,调用node.getChildNodes()获取该节点的子节点,文本节点也会被当作一个Node来返回,如:
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
输出的结果是:
1 2 3 4 5 |
|
其中\n的ASCII码为10,\t的ASCII码为9。结果让人大吃一惊,university的子节点数不是1,也不是2,而是3,这3个子节点都是谁呢?为了看得更清楚点,把xml文档改为:
1 2 3 4 5 6 7 8 9 10 |
|
还是上面的程序,输出结果为:
1 2 3 4 5 |
|
其中数字1的ASCII码为49,数字2的ASCII码为50。
2、使用SAX来解析同DOM,当你重写它的public void characters(char[] ch, int start, int length)方法时,你就能看到。
3、JDOM,调用node.getChildren()只返回子节点,不包括TextNode节点(不管该节点是否有Text信息)。如果要获取该节点的Text信息,可以调用node.getText()方法,该方法返回节点的Text信息,也包括\n\t等特殊字符。
4、DOM4j同JDOM
The above is the detailed content of Detailed introduction to the sample code of four methods of parsing Xml. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and
