Java XML Parsing tool dom4j Introduction and use instance Java XML Parsing tool dom4j introduction and use instance
DOM4J Introduction
DOM4J's project address: http://sourceforge.net/projects/dom4j/?source=directory
DOM4J is a simple open source library for working with XML, XPath, and XSLT, which is based on the Java platform and integrates the Dom,sax and JAXP with the Java Collection framework.
Use of dom4j
After downloading the DOM4J project, unzip and add its jar package (my current version is called Dom4j-1.6.1.jar) to the following class path.
(Properties->java Build Path, Add External JARs ... )。
It can then be programmed using the APIs it provides.
program Example 1
The first program, which generates an XML document with Java code, is as follows:
Package Com.example.xml.dom4j;import Java.io.fileoutputstream;import Java.io.filewriter;import org.dom4j.Document; Import Org.dom4j.documenthelper;import Org.dom4j.element;import Org.dom4j.io.outputformat;import org.dom4j.io.xmlwriter;/** * DOM4J Framework Learn to create an XML document using the DOM4J framework and output Save * */public class dom4jtest1{public static void main (Stri Ng[] args) throws Exception {//The first way: Create a document and create a root element//Create a document: Use a helper class document document = Docume Nthelper.createdocument (); Create the root node and add the document Element root = documenthelper.createelement ("student"); Document.setrootelement (root); The second way: Create a document and set the root element node of the document elements Root2 = documenthelper.createelement ("student"); Document Document2 = documenthelper.createdocument (ROOT2); Add attribute Root2.addattribute ("name", "Zhangsan"); Add child nodes: Add to return the element helloelement = root2.addelement ("Hello"); Element worldelement = root2.addelement ("World"); Helloelement.settext ("Hello Text"); Worldelement.settext ("World text"); Output//Output to console XMLWriter XMLWriter = new XMLWriter (); Xmlwriter.write (document); Output to File//format OutputFormat format = new OutputFormat ("", true);//Set indent to 4 spaces, and another line is true Xmlwrite R xmlWriter2 = new XMLWriter (New FileOutputStream ("Student.xml"), format); Xmlwriter2.write (Document2); Another way of output, remember to call the Flush () method, otherwise the output file shows blank XMLWriter XmlWriter3 = new XMLWriter (New FileWriter ("Student2.xml"), format); Xmlwriter3.write (Document2); Xmlwriter3.flush (); The close () method can also}}
Program Console output:
<?xml version= "1.0" encoding= "UTF-8"?><student/>
An XML document that is generated:
<?xml version= "1.0" encoding= "UTF-8"? ><student name= "Zhangsan" >
program Example 2Program Example 2, read into the XML document and analyze, output its contents.
First, the documents to be analyzed are as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><students name= "Zhangsan" >
Java code:
Package Com.example.xml.dom4j;import Java.io.file;import Java.util.iterator;import java.util.list;import Javax.xml.parsers.documentbuilder;import Javax.xml.parsers.documentbuilderfactory;import org.dom4j.Document; Import Org.dom4j.element;import org.dom4j.io.domreader;import org.dom4j.io.saxreader;/** * dom4j Framework Learning: Read and parse XML * * */PU Blic class dom4jtest2{public static void Main (string[] args) throws Exception {Saxreader Saxreader = new SA Xreader (); Document document = Saxreader.read (new File ("Students.xml")); Gets the root elements element root = Document.getrootelement (); System.out.println ("Root:" + root.getname ()); Gets all child elements list<element> childlist = Root.elements (); SYSTEM.OUT.PRINTLN ("Total child count:" + childlist.size ()); Gets the child element of a specific name list<element> ChildList2 = root.elements ("Hello"); System.out.println ("Hello Child:" + childlist2.size ()); Gets the first child element whose name is the specified name of element FirstworldeLement = Root.element ("World"); Output its properties System.out.println ("First World Attr:" + firstworldelement.attribute (0). GetName () + "=" + firstworldelement.attributevalue ("name")); System.out.println ("Iterative output-----------------------"); Iterate output for (Iterator iter = Root.elementiterator (); Iter.hasnext ();) {element e = (Element) Iter.next (); System.out.println (E.attributevalue ("name")); } System.out.println ("with Domreader-----------------------"); Documentbuilderfactory dbf = Documentbuilderfactory.newinstance (); Documentbuilder db = Dbf.newdocumentbuilder (); Note To use the full class name org.w3c.dom.Document Document2 = db.parse (New File ("Students.xml")); Domreader Domreader = new Domreader (); Convert JAXP document to dom4j document document DOCUMENT3 = Domreader.read (Document2); Element rootelement = Document3.getrootelement (); System.out.println ("RooT: "+ rootelement.getname ()); }}Output after code runs:
Root:studentstotal Child Count:6hello Child:3first World attr:name= Wangwu iterative output-----------------------lisilisi2lisi3wangwuwangwu2null with Domreader-----------------------root:students
ResourcesSanthiya Garden Zhang Long teacher XML Video tutorial.
Source code Download: http://sourceforge.net/
Search Dom4j:http://sourceforge.net/projects/dom4j/?source=directory
DOM4 parsing XML