Java uses XPath and Dom4j to parse XML

1. Four methods of XML file parsing

There are usually four classical methods for parsing XML files. There are two basic parsing methods: sax and DOM. Sax is based on event flow parsing, and DOM is based on XML document tree structure parsing. On this basis, in order to reduce the encoding amount of DOM and sax, JDOM appears. Its advantages are:, The 20-80 principle (Pareto rule) greatly reduces the amount of code. Generally, JDOM meets the requirements of simple functions to be realized, such as parsing and creation. However, at the bottom, JDOM still uses Sax (the most commonly used), DOM and xanan documents. The other is Dom4j, which is an excellent Java XML API with excellent performance, powerful functions and extremely easy to use. At the same time, it is also an open source software. Now you can see that more and more Java software are using Dom4j to read and write XML. In particular, it is worth mentioning that even sun's JAXM is also using D OM4J。 The specific use of the four methods will be introduced in detail by Baidu. 2 brief introduction to XPath

XPath is a language for finding information in XML documents. XPath is used to navigate through elements and attributes in XML documents and traverse elements and attributes. XPath is the main element of W3C XSLT standard, and XQuery and xpointer are built on XPath expression at the same time. Therefore, the understanding of XPath is the basis of many advanced XML applications. XPath is very similar to the SQL language for database operation, or jQuery. It can facilitate developers to grab what they need in documents. Dom4j also supports the use of XPath. 3 Dom4j using XPath

When DOM 4J uses XPath to parse XML documents, it first needs to reference two jar packages in the project:

dom4j-1.6. 1. Jar: Dom4j package, download address http://sourceforge.net/projects/dom4j/ ;

jaxen-xx. xx. Jar: this package is usually not added, and an exception will be thrown (java.lang.noclassdeffounderror: org / jaxen / jaxenexception). Download address: http://www.jaxen.org/releases.html 。

3.1 namespace interference

When dealing with XML files converted from Excel files or other format files, you usually encounter the situation that you can't get results through XPath parsing. This is usually due to the existence of namespaces. Take the XML file with the following content as an example. You can simply retrieve it through XPath = "/ / workbook / worksheet / table / row [1] / cell [1] / data [1]". Usually, there is no result. This is caused by the namespace namespace (xmlns = "urn: schemas microsoft com: Office: spreadsheet").

Online XML / JSON conversion tool: http://tools.jb51.net/code/xmljson

Format XML online / compress XML online: http://tools.jb51.net/code/xmlformat

XML online compression / formatting tool: http://tools.jb51.net/code/xml_format_compress

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>