Talking about parsing XML document with Java (IV.)

Source: Internet
Author: User

Continue with the article, this a while because of the company project to intensify, resulting in the last way to parse the XML document, has not been summarized, the following summarizes the use of DOM4J parsing XML.

DOM4J (Document Object Model for Java)

Using dom4j parsing, personally considered to be the most convenient one of four analytic methods, dom4j is a very good Java XML API, with excellent performance, powerful and extremely easy to use features, but 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.

Let's look at our resolution steps below:

(i), create Saxreader object reader

Saxreader Saxreader = new Saxreader ();

(b), through the Read method of the Saxreader object, parse the XML out and obtain a Document object

Document document = Saxreader.read (new File ("books.xml"));

(iii) Obtaining the root node of an XML document through the Document object

Element element = Document.getrootelement ();

(iv), through the iterator through the resulting root node, the detailed code is as follows:

      

 Public Static voidMain (string[] args) {Saxreader reader=NewSaxreader (); Try{Document Document= Reader.read (NewFile ("books.xml")); Element Books=document.getrootelement (); Iterator it=Books.elementiterator ();  while(It.hasnext ()) {System.out.println ("Start Traversal"); Element element1=(Element) it.next (); List<Attribute> list =element1.attributes ();  for(Attribute attr:list) {System.out.println ("Property Name:" +attr.getname () + "----Property value:" +Attr.getvalue ()); //get child nodes under the root node} Iterator ITT=Element1.elementiterator ();  while(Itt.hasnext ()) {Element Bookchild=(Element) itt.next (); System.out.println ("Node Name:" + bookchild.getname () + "--node value:" +Bookchild.getstringvalue ()); }            }        } Catch(documentexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }

The resulting result is:

    

start Traversal property named: ID----attribute value is: 1 node Name: Name-- node value: Old Man and Sea node name: Author-- node value: Hemingway Day Name:year-- Node Value: 1955 node Name:Price--node value: start Traversal property named: ID----attribute value: 2 node Name: Name-- node value: Book Swordand the name of the node: year- -Node Value: 1959 node Name:Price--node value: node name: Language--node value: Chinese

The parsed XML document is: Books.xml

<?XML version= "1.0" encoding= "UTF-8"?><Bookstore>    < BookID= "1">        <name>The old man and the sea</name>        <author>Hemingway</author>        < Year>1955</ Year>        < Price>45</ Price>    </ Book>    < BookID= "2">        <name>A book and a sword</name>        < Year>1959</ Year>        < Price>24</ Price>        <language>Chinese</language>    </ Book>    </Bookstore>

  Summarize:   

Although DOM4J represents a completely independent development result, 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.

The goal of DOM4J is the same as jdom when adding flexibility, XPath integration, and the goal of large document processing: 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.

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.

Comparison of the above methods :

1. 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.

2. Jdom and Dom perform poorly during performance testing, and memory overflows when testing 10M documents, but portable . In the case of small documents it is also worth considering DOM and Jdom. Although the developers of Jdom have stated that they expect to focus on performance issues before the full release, there is really no merit in the performance point of view. In addition, Dom is still a very good choice. 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).

3. SAX behaves better, depending on its specific parsing mode-event driven. 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).

my view: dom4j is recommended if the XML document is large and does not consider portability, and if the XML document is smaller it is recommended to use Jdom and sax if you need to deal with it in a timely manner without having to save the data. But in any case, or that sentence: suitable for their own is the best, if time permitting, we suggest that all four ways to try and then choose a suitable for their own.

Reference article: http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/17/3082949.html

Talking about parsing XML document with Java (IV.)

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.