[Java technology]-Comparison of XML parsing methods and xml parsing in java

Source: Internet
Author: User

[Java technology]-Comparison of XML parsing methods and xml parsing in java

Why?

"It can be used to mark and define data types. It is a source language that allows users to define their own markup language. It is ideal for World Wide Web transmission and provides a unified way to describe and exchange structured data independent of applications or vendors. It is a cross-platform and content-dependent Technology in the Internet environment and an effective tool for processing distributed structure information ." XML is designed to transmit and store data;

Its platform independence, language independence, and system independence bring great convenience to data inheritance and interaction. Therefore, XML is increasingly common.

 

Method

1. DOM -- incluentobject Model

Like HTML operations, I have learned how to use DOM in Javascript. It is a standard programming interface recommended by W3C for processing Extensible Markup Language. The DOM parser converts an XML document into a tree structure and can traverse the tree.

Advantage: Easy to operate. Developers only need to call the build instruction. The entire document tree is in the memory, and then use navigationAPIs to delete, modify, rearrange, and other functions.

Disadvantage: It takes resources (time and space) to load the entire document to the memory ). The XML file will be read and saved to the memory at a time, and then parsed. If the file is large, more content needs to be read and the efficiency is low.

Application scenarios: the XML document data needs to be accessed multiple times for one resolution. The hardware resources (memory and CPU) are sufficient ).

 

 

2. SAX -- SimpleAPI for XML

SAX is based on event stream parsing. It is a "push" model used to process XML event-driven. Similar to a media stream, the SAX Parser can start a series of events when parsing XML, developers write Response Event code to save data.

Advantage: it solves the resource-consuming problem of DOM loading large files.

Disadvantage; it is also characteristic of its shortcomings. It must implement multiple event handlers to process all the arrival times, and the logic processing is more complicated.

 

3. JDOM -- JavaDocument Object Model

As the name implies: Java-specific document models use the law principle to greatly reduce the amount of code and effectively combine the functions of SAX and DOM. The underlying layer is still implemented using DOM and SAX.

 

4. DOM4j

Dom4j is an easy-to-use and open-source library for XML, XPath, and XSLT. It is applied to the Java platform. It adopts the Java Collection framework and fully supports DOM, SAX, and JAXP.

The project simply tries to parse XML using DOM4j. The general steps for reading are as follows:


// SAXReader reader = new SAXReader () in the SAX mode; // get the current Thread through Thread and get the loader ClassLoader and getResource... (load by resource) Get the input stream InputStream in = Thread. currentThread (). getContextClassLoader (). getResourceAsStream ("relative resource path"); Document dc = reader. read (in); // obtain the Element ElementElement driverNameElt = (Element) dc Through XPath. selectObject ("/config/db-info/driver-name ");


Summary

DOM and SAX are two basic XML parsing methods, and the other two are further encapsulated in DOM and SAX.

The importance of XML is becoming more and more apparent. to make the system more flexible, configuration files are everywhere, and you are not familiar with XML operations. Today I have summarized several methods for operating XML in Java, I also hope to be more proficient and profound in the following learning applications.

 

References:

Detailed description and comparison of four xml operations (DOM, SAX, JDOM, and DOM4J) in java

Four Methods for parsing XML in Java

Several Methods for parsing XML in java

Http://www.w3.org/

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.