Introduction to common methods of XML creation and parsing

Source: Internet
Author: User

XmlIntroduction to Analytic methods

1.DOM4J (Document Object Model for Java)
Although DOM4J represents a completely independent development result, but initially it is an intelligent branch of Jdom. It incorporates a number of features beyond the basic XML document representation, including integrated XPath support, XML schema support, and event-based processing for large documents or streaming documents. It also provides options for building a document representation, which has parallel access through the DOM4J API and the standard DOM interface. It has been in development since the second half of 2000.

to support all of these features, DOM4J uses interfaces and abstract basic class methods. DOM4J uses the collections class in the API extensively, but in many cases it also provides workarounds to allow for better performance or more direct encoding methods. The direct benefit is that while DOM4J has paid the cost of a more complex API, it offers much greater flexibility than jdom.

in adding flexibility, When it comes to XPath integration and the goal of large document processing, DOM4J's goal is the same as Jdom: ease of use and intuitive operation for Java developers. It is also committed to becoming a more complete solution than jdom, achieving the goal of essentially addressing all java/xml issues. When this goal is completed, it is less stressed than jdom to prevent incorrect application behavior.

Dom4j is a very, very good Java XML API that features excellent performance, power, and extreme ease of use, and is also an open source software. Now you can see that more and more Java software is using dom4j to read and write XML, especially to mention that even Sun's JAXM is using DOM4J.

Advantages
① uses a large number of Java collection classes to facilitate Java developers, while providing some alternative approaches to high performance.
② supports XPath.
The ③ has good performance.

Disadvantages
① uses a lot of interfaces, and the API is more complex.

2.SAX(simpleAPI for XML)

The advantages of Sax processing are very similar to the advantages of streaming media. The analysis can begin immediately, rather than waiting for all data to be processed. Also, because the application examines data only when it is being read, it does not need to store the data in memory. This is a huge advantage for large documents. In fact, the application doesn't even have to parse the entire document; it can stop parsing when a condition is met. In general, Sax is much faster than its surrogate dom.

Select Dom or choose Sax? Choosing the DOM or Sax parsing model is a very important design decision for developers who need to write their own code to handle XML documents. The DOM accesses the XML document in a tree-structured way, and Sax uses the event model.

The DOM parser transforms an XML document into a tree containing its contents and can traverse the tree. The advantage of parsing a model with DOM is that programming is easy, and developers simply need to invoke the build instructions and then use the navigation APIs to access the desired tree nodes to complete the task. It is easy to add and modify elements in the tree. However, because of the need to process the entire XML document when using the DOM parser, the performance and memory requirements are high, especially when encountering large XML files. Because of its traversal capabilities, DOM parsers are often used in services where XML documents require frequent changes.

The SAX parser uses an event-based model that can trigger a sequence of events when parsing an XML document, and when a given tag is found, it can activate a callback method that tells the method that the label has been found. Sax's requirements for memory are usually low because it lets the developer decide which tag to process. Especially if the developer only needs to work with some of the data contained in the document, Sax has a better ability to scale. But it is difficult to code with a SAX parser, and it is difficult to access multiple different data in the same document at the same time.

Advantage
① does not need to wait for all data to be processed, the analysis can begin immediately.
② only examines data when it is read, and does not need to be stored in memory.
③ can stop parsing when a condition is met, without having to parse the entire document.
④ high efficiency and performance to parse documents larger than system memory.

Disadvantages
① requires the application to be responsible for the processing logic of the tag (for example, maintaining a parent/child relationship, etc.), and the more complex the document is.
② One-way navigation, cannot locate the document hierarchy, it is difficult to access different parts of the same document at the same time, XPath is not supported.

3.DOM(Document Object Model)

DOM is the official standard for representing XML documents in a platform-and language-neutral way. The DOM is a collection of nodes or pieces of information that are organized in a hierarchical structure. This hierarchy allows developers to look for specific information in the tree. Analyzing the structure usually requires loading the entire document and constructing the hierarchy before any work can be done. Because it is based on the information hierarchy, the DOM is considered to be tree-based or object-based.

Advantages
① allows applications to make changes to data and structures.
② access is bidirectional and can be navigated up and down in the tree at any time, capturing and manipulating any part of the data.
Disadvantages
① usually needs to load the entire XML document to construct the hierarchy, consuming resources.

XmlComparison of parsing methods

No1. DOM4J performance is the best, even Sun's JAXM is also in use dom4j. many open source projects currently employ dom4j, such as the famous Hibernate, which also uses DOM4J to read XML configuration files. If portability is not considered, then dom4j is used.

No2. Sax behaves better, depending on its specific parsing-event-driven behavior. A sax detects the incoming XML stream, but does not load into memory (of course, some documents are temporarily hidden in memory when the XML stream is read).

No3. The DOM performs poorly in performance testing, and memory overflows when testing 10M documents, but is portable. DOM implementations are widely used in many programming languages. It is also the basis for many other XML-related standards, because it is formally recommended (as opposed to a non-standard Java model), so it may also be needed in some types of projects (such as using the DOM in JavaScript).

XML Operation Instance

Attention:

To add a jar to the classpath step is roughly the following:

1. Create a lib folder and paste all the customizations into it;

2. Select the project right click, select Configure build path, open the Java Build Path dialog box

3. Open the Add custom Library dialog box

At this point, you are ready for development!

XmlDevelopment Examples

Source Address: Https://github.com/Rick-bao/tools.git

1. dom4j (http://dom4j.github.io/)

Element.elements (String elementnode); --Find the element collection under the node tree List

Element.elementtexttrim (String elementnode); -- find the values under the node tree

Element.attributevalue (String elementnode); -- Find the attribute values under the node tree

The above method is a personal hand-written verification results. Personally, the Dom tree can be used as a node-by-layer operation, or a single sub-node operation, the deconstruction of the design is more ideal.

2.SAX (http://www.saxproject.org/about.html)

DefaultHandler- Event-driven

Startdocument-- start parsing method

Enddocument-- End Method

Startelement-- start node tree

Characters-- parsing character Set

EndElement-- End node tree

3.DOM (http://mvnrepository.com/artifact/xml-apis/xml-apis)

NodeList-- node collection interface

Childnodes.item (j). Getnodename ()-- Get node name

Childnodes.item (j). Getfirstchild (). Getnodevalue ()-- node content

Note:

Source Address: Https://github.com/Rick-bao/tools.git

Document address: Under Project documents directory.

Introduction to common methods of XML creation and parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.