Four methods of parsing XML in Java
XML has now become a general data exchange format. Its platform independence, language independence and system independence bring great convenience to data integration and interaction. For the syntax knowledge and technical details of XML itself, you need to read relevant technical documents, including DOM (document object model), DTD (document type finalization), Sax (simple API for XML), XSD (XML schema finalization) and XSLT (extensible stylesheet language transformations). For details, please refer to the documents on the W3C official website http://www.w3.org For more information.
XML is parsed in the same way in different languages, but the syntax is different. 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. Suppose that the content and structure of our XML are as follows:
This paper uses Java language to realize XML document generation and parsing of DOM and Sax.
First, define an interface XmlDocument for operating XML documents, which defines the interface for establishing and parsing XML documents.
1. DOM generates and parses XML documents
A set of interfaces is defined for the parsed version of an XML document. The parser reads the entire document, then builds a memory resident tree structure, and then the code can manipulate the tree structure using the DOM interface. Advantages: the whole document tree is in memory and easy to operate; Support deletion, modification, rearrangement and other functions; Disadvantages: transferring the whole document into memory (including useless nodes) wastes time and space; usage: once the document is parsed, you need to access these data many times; hardware resources are sufficient (memory and CPU).
2. Sax generates and parses XML documents
In order to solve the problem of DOM, Sax appears. Sax, event driven. When the parser finds the start or end of an element, the beginning or end of a text or document, it sends events. The programmer writes code to respond to these events and saves the data. Advantages: there is no need to transfer in the whole document in advance, which takes up less resources; Sax parser code is smaller than DOM parser code, which is suitable for applet download. Disadvantages: not persistent; After the event, if the data is not saved, the data will be lost; Statelessness; You can only get text from the event, but you don't know which element the text belongs to; Usage: applet; Only a small amount of content of XML document is needed, and there is little back access; Less machine memory;
3. Dom4j generates and parses XML documents
Dom4j is a very, very excellent Java XML API. It has the characteristics of excellent performance, powerful function 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 Dom4j.
4. JDOM generates and parses XML
In order to reduce the encoding amount of DOM and sax, JDOM appears; Advantages: 20-80 principle, greatly reducing the amount of code. Usage: the functions to be implemented are simple, such as parsing and creation, but at the bottom, JDOM still uses Sax (the most commonly used), DOM and xanan documents.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.