1. dom4j Overview
Dom4jIs an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java collections framework and with full support for Dom, Sax and JAXP.
Dom4j Official Website: dom4j
Dom4j source code download: dom4j download
In this example, you must import the dom4j. jar package to reference the dom4j class,For the dom4j source code and jar package, see [Source Code download] In this example or visit dom4j
The Org. dom4j package not only contains the builder class for creating XML, but also contains the parser saxreader and element for parsing XML, including the following classes:
Org. dom4j
Org. dom4j. extends enthelper;
Org. dom4j. element;
Org. dom4j. Io. saxreader;
Org. dom4j. Io. xmlwriter;
Org. dom4j. extends entexception;
SDK source code viewing path(Google Code)
Create and parse XML:
2. Create XML in dom4j
Dom4j mainly uses org. dom4j.DocumenthelperOrg. dom4j. Document, org. dom4j. Io. outputformat, org. dom4j. Io. xmlwriter
First, compile enthelper. createdocument () to create the instance Doc for org. dom4j. Document.
Then, set the XML attributes Doc. setxmlencoding ("UTF-8"), Doc. addelement ("root") root nodes, and sub-nodes through Doc.
Then, define the XML format and output the new xmlwriter (xmlwriter, outputformat)
Code
/** Create XML in dom4j mode */<br/> Public String dom4jxmlcreate () {<br/> stringwriter xmlwriter = new stringwriter (); </P> <p> person [] persons = new person [3]; // create a node person object <br/> persons [0] = new person (1, "sunboy_2050", "http://blog.csdn.net/sunboy_2050"); <br/> persons [1] = new person (2, "Baidu", "http://www.baidu.com "); <br/> persons [2] = new person (3, "Google", "http://www.google.com"); </P> <p> try {<br/> Org. dom4j. document Doc = incluenthelper. createdocument (); </P> <p> Doc. setxmlencoding ("UTF-8"); </P> <p> Org. dom4j. element eleroot = Doc. addelement ("root"); <br/> eleroot. addattribute ("author", "Homer"); <br/> eleroot. addattrit ("date", "2012-04-25"); <br/> eleroot. addcomment ("dom4j test"); </P> <p> int personslen = persons. length; <br/> for (INT I = 0; I <personslen; I ++) {</P> <p> element eleperson = eleroot. addelement ("person"); // create a person node. The reference class is Org. dom4j. element </P> <p> element eleid = eleperson. addelement ("ID"); <br/> eleid. addtext (persons [I]. GETID () + ""); </P> <p> element elename = eleperson. addelement ("name"); <br/> elename. addtext (persons [I]. getname (); </P> <p> element eleblog = eleperson. addelement ("blog"); <br/> eleblog. addtext (persons [I]. getblog (); <br/>}</P> <p> Org. dom4j. io. outputformat = new Org. dom4j. io. outputformat (); // set the XML output format <br/> outputformat. setencoding ("UTF-8"); <br/> outputformat. setindent (false); <br/> outputformat. setnewlines (true); <br/> outputformat. settrimtext (true); </P> <p> Org. dom4j. io. xmlwriter output = new xmlwriter (xmlwriter, outputformat); // save XML <br/> output. write (DOC); <br/> output. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> savedxml (filename, xmlwriter. tostring (); <br/> return xmlwriter. tostring (); <br/>}
Running result:
3. dom4j parses XML
Dom4j and XML parsing mainly use org. dom4j. Io. saxreader, org. dom4j. Document, Doc. getrootelement (), and ELE. getname () and ELE. gettext ().
First, create an instance reader for saxreader to read the XML byte stream reader. Read (is)
Then, get the root node through Doc. getrootelement (), and use the iterator to get the lower-level sub-node eleroot. elementiterator () of root.
Then, the parsed XML content xmlwriter. append (xmlheader) and xmlwriter. append (personslist. Get (I). tostring () are obtained ())
Resolution 1: Standard resolution (Iterator Iteration)
Code
/** Parse XML in dom4j mode */<br/> Public String dom4jxmlresolve () {<br/> stringwriter xmlwriter = new stringwriter (); </P> <p> inputstream is = readxml (filename); <br/> try {<br/> saxreader reader = new saxreader (); <br/> Org. dom4j. document Doc = reader. read (is); </P> <p> List <person> personslist = NULL; <br/> person = NULL; <br/> stringbuffer xmlheader = new stringbuffer (); </P> <p> element eleroot = Doc. getrootelement (); // obtain the root node. The reference class is Org. dom4j. element <br/> string attrauthor = eleroot. attributevalue ("author"); <br/> string attrdate = eleroot. attributevalue ("date"); <br/> xmlheader. append ("root "). append ("\ t"); <br/> xmlheader. append (attrauthor ). append ("\ t"); <br/> xmlheader. append (attrdate ). append ("\ n"); <br/> personslist = new arraylist <person> (); </P> <p> // obtain the root subnode, that is, person <br/> iterator <element> iter = eleroot. elementiterator (); <br/> for (; ITER. hasnext ();) {<br/> element eleperson = (element) ITER. next (); </P> <p> If ("person ". equals (eleperson. getname () {<br/> person = new person (); </P> <p> // get the person subnode, that is, ID, name, blog <br/> iterator <element> inneriter = eleperson. elementiterator (); <br/> for (; inneriter. hasnext ();) {<br/> element ele = (element) inneriter. next (); </P> <p> If ("ID ". equals (Ele. getname () {<br/> string id = ELE. gettext (); <br/> person. setid (integer. parseint (ID); <br/>} else if ("name ". equals (Ele. getname () {<br/> string name = ELE. gettext (); <br/> person. setname (name); <br/>} else if ("blog ". equals (Ele. getname () {<br/> string blog = ELE. gettext (); <br/> person. setblog (blog); <br/>}</P> <p> personslist. add (person); <br/> person = NULL; <br/>}</P> <p> xmlwriter. append (xmlheader); <br/> int personslen = personslist. size (); <br/> for (INT I = 0; I <personslen; I ++) {<br/> xmlwriter. append (personslist. get (I ). tostring (); <br/>}</P> <p >}catch (incluentexception e) {<br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return xmlwriter. tostring (); <br/>}
Running result:
Resolution 2: Selective resolution (XPath path)
Dom4j + XPath, select only ID resolution,Doc. selectnodes ("// root // person // ID ")
Code
/** Parse XML in dom4j mode (method 2) */<br/> Public String dom4jxmlresolve2 () {<br/> stringwriter xmlwriter = new stringwriter (); </P> <p> inputstream is = readxml (filename); <br/> try {<br/> Org. dom4j. io. saxreader reader = new Org. dom4j. io. saxreader (); <br/> Org. dom4j. document Doc = reader. read (is); </P> <p> List <person> personslist = NULL; <br/> person = NULL; <br/> stringbuffer xmlheader = new stringbuffer (); </P> <p> element eleroot = Doc. getrootelement (); // obtain the root node. The reference class is Org. dom4j. element <br/> string attrauthor = eleroot. attributevalue ("author"); <br/> string attrdate = eleroot. attributevalue ("date"); <br/> xmlheader. append ("root "). append ("\ t"); <br/> xmlheader. append (attrauthor ). append ("\ t"); <br/> xmlheader. append (attrdate ). append ("\ n"); <br/> personslist = new arraylist <person> (); </P> <p> @ suppresswarnings ("unchecked ") <br/> List <element> idlist = (list <element>) Doc. selectnodes ("// root // person // ID"); // select all IDs <br/> iterator <element> iditer = idlist. iterator (); <br/> while (iditer. hasnext () {<br/> person = new person (); </P> <p> element idele = (element) iditer. next (); <br/> string id = idele. gettext (); <br/> person. setid (integer. parseint (ID); </P> <p> personslist. add (person); <br/>}</P> <p> xmlwriter. append (xmlheader); <br/> int personslen = personslist. size (); <br/> for (INT I = 0; I <personslen; I ++) {<br/> xmlwriter. append ("ID = "). append (personslist. get (I ). GETID () + ""). append ("\ n"); <br/>}</P> <p >}catch (condition entexception e) {<br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return xmlwriter. tostring (); <br/>}
Note:When parsing XML with XPath, You need to import jaxen;This example needs to import the latest jaxen package jaxen-1.1.3.jar, you can download this example below [Source Code download] or access jaxen jar
JaxenIs an open source XPath library written in Java. it is adaptable to partition different object models, including Dom, XOM, dom4j, and JDOM. is it also possible to write adapters that treat non-XML trees such as compiled JAVA byte code or Java Beans as XML, thus enabling you to query these trees with XPath too.
Official jaxen Website: jaxen
Download the jar package from jaxen: jaxen jar or jaxen jar
View jaxen source code: jaxen SRC or jaxen trunk
Running result:
4. Person class
See the previous blog Android creation and parsing XML (2) -- DOM mode [4, person class]
Source code download
Reference recommendations:
Dom4j (official website)
Dom4j SRC (source code download)
Dom4j SRC and jar (Google Code)
Jaxen (Official jaxen website)
Jaxen jar (jaxen jar package download)
Jaxen SRC (jaxen online source code)
Dom4j parses XML (IBM)
Dom4j and XPath Parse XML
Dom4j attribute value line breaks