XML Parser--dom4j

Source: Internet
Author: User
Tags xml parser xpath

4)dom4j http://dom4j.sourceforge.net

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 for 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 the year.  

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 alternatives 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 more than JDOM much greater flexibility.  

The goal ofdom4j 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.  

Dom4j is a very, very good Java XML APIthat 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, and it's worth mentioning that even Sun JAXM is also using dom4j.



Package Com.lr.utils;import Java.io.file;import Java.io.ioexception;import java.util.arraylist;import Java.util.iterator;import Java.util.list;import Javax.xml.parsers.documentbuilderfactory;import Javax.xml.parsers.parserconfigurationexception;import Org.dom4j.attribute;import Org.dom4j.Element;import Org.dom4j.io.domreader;import Org.dom4j.document;import Org.xml.sax.saxexception;import Com.lr.bean.BookInfo; public class Dom4j {//common method for obtaining all book information @suppresswarnings ("unchecked") publicly list<bookinfo> GetInfo () {list< Bookinfo> list=new arraylist<bookinfo> (); try {//dom parsing domreader reader=new domreader ();D Ocumentbuilderfactory factory=documentbuilderfactory.newinstance ();//Create a document tree through the Documentation builder documents doc= Reader.read (Factory.newdocumentbuilder (). Parse (New File ("books.xml")); Element rootelement=doc.getrootelement ();//Gets the root element//Iteration Value iterator It=rootelement.elementiterator (); Object ele; BookInfo Bk=null; Element e; Attribute Attr;while (It.hasnext ()) {//contains child nodes Ele=it.next (), if ("book". EQUals ((Element) ele). GetName ()) {//element name equals Bookbk=new BookInfo ();//Get all the properties of a book for (Iterator attrlist= ( org.dom4j.Element) ele). Attributeiterator (), Attrlist.hasnext ();) {attr= (Attribute) Attrlist.next (), if ("id". Equals (Attr.getname ())) {Bk.setid (Attr.getvalue ());}} Gets all child nodes of the book for (Iterator el= ((org.dom4j.Element) ele). Elementiterator (); El.hasnext ();) {e= (Element) el.next (); if (" BookName ". Equals (E.getname ())) {Bk.setbookname (((org.dom4j.Element) e). Gettexttrim ());} else if ("Price". Equals (E.getname ())) {Bk.setprice (((org.dom4j.Element) e). Gettexttrim ());} else if ("Bookpub". Equals (E.getname ())) {Bk.setbookpub (((org.dom4j.Element) e). Gettexttrim ());}} List.add (BK);}} catch (Saxexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} catch ( Parserconfigurationexception e) {e.printstacktrace ();} return list;} public static void Main (string[] args) {dom4j d=new dom4j (); System.out.println (D.getinfo ());}}



XML Parser--dom4j

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.