DOM4J is the XML API for java, which is used to read and write XML files. There are many scenarios where dom4j is used to read and write Xml.
To use dom4j development, you need to download the appropriate JAR file for import dom4j.
Official website Download: HTTP://WWW.DOM4J.ORG/DOM4J-1.6.1/
GitHub download: http://dom4j.github.io/
After downloading the extract:
We just need to build the Dom4j-1.6.1.jar file into our development project.
The following is an example of how eclipse creates a Java project:
Disclaimer: development environment for this Java Project Jdk1.8,eclipse version neon.1 release (4.6.1)
First create a demo project:
Create a Lib file in the demo project, copy the Dom4j-1.6.1.jar file to lib, and then right-click the Dom4j-1.6.1jar file. :
Click Add to Bulid the path to build into the Project.
Import succeeded:
In the process of project development, you can refer to the Docs folder (help document), find index.html open, Click on Quick Start can be used to help the document to learn dom4j XML parsing.
:
Below I will introduce a detailed example of the Java operation XML file, the file name is Exmple.java.
1 packagevastsum;2 3 Importjava.io.File;4 Importjava.util.Iterator;5 Importjava.util.List;6 7 Importorg.dom4j.Document;8 Importorg.dom4j.DocumentException;9 Importorg.dom4j.Element;Ten Importorg.dom4j.Node; one Importorg.dom4j.io.SAXReader; a - /** - * Read the next XML document to get the document Object. the * This article for the first XML series, the following code can be run directly, the end of the attached source Code. - */ - classExmple { - public Static voidMain (string[] Args)throwsdocumentexception + { -Saxreader reader =NewSaxreader (); +Document document = Reader.read (NewFile ("./src/contact.xml"))); a at /** - * How to manipulate node objects - */ - - //Get document root node -Element root =document.getrootelement (); in //the name of the output root tag - System.out.println (root.getname ()); to + - //gets all child nodes under the root node (not child nodes that have been wrapped) thelist<element> list =root.elements (); * //ways to traverse the list $ for(Element E:list) {Panax Notoginseng System.out.println (e.getname ()); - } the + a //Gets the child nodes below the specified node theElement Contactelem = root.element ("contact");//The first thing you need to know is the node you want to Manipulate. +List<element> contactlist =contactelem.elements (); - for(Element E:contactlist) { $ System.out.println (e.getname ()); $ } - - the //call the following to get the recursive function of the child Node. - getchildnodes (root);Wuyi the - //gets the first sub-label of the specified name under the current label wuElement Conelem = root.element ("contact"); - System.out.println (conelem.getname ()); about $ - //get a deeper label ( one layer at a Level) -Element Nameelem = root.element ("contact"). element ("name")); - System.out.println (nameelem.getname ()); a } + the //recursive query node function, output node name - Private Static voidgetchildnodes (Element Elem) { $ System.out.println (elem.getname ()); theIterator<node> it=Elem.nodeiterator (); the while(it.hasnext ()) { theNode node =It.next (); the if(nodeinstanceofElement) { -Element e1 =(Element) node; in getchildnodes (e1); the } the about } the } the the +}
The following is the corresponding XML file, the file name is Contact.xml:
1 <?XML version= "1.0" encoding= "utf-8"?>2 <contactlist>3 < contactID= "001"class= "style">4 <name>Tom</name>5 < age>20</ age>6 <Phone>134222223333</Phone>7 <Email>[email protected]</Email>8 <QQ>432221111</QQ>9 </ contact>Ten < contactID= "002"> one <name>John doe</name> a < age>20</ age> - <Phone>134222225555</Phone> - <Email>[email protected]</Email> the <QQ>432222222</QQ> - </ contact> - <Contacttwo> - <name>Harry</name> + < age>32</ age> - <Phone>465431341</Phone> + <Emali>[email protected]</Emali> a <QQ>46164694</QQ> at </Contacttwo> - <Test>Test</Test> - <Test>Other uses</Test> - </contactlist>
The directory for the two files is shown in Title:
Java Fetch XML node summary read XML document node