dom4j is Java's XML API, used to read and write XML files. Currently, there are many scenarios that use dom4j to read and write XML.
To use dom4j development, you need to download and import the corresponding jar file for dom4j.
Official website download: http://www.dom4j.org/dom4j-1.6.1/
github download: http://dom4j.github.io/
After downloading and decompressing, as shown in the figure:
We just need to build the dom4j-1.6.1.jar file into our development project.
Here is an example of how to create a Java project in Eclipse:
Statement: 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. As shown in the figure:
Click Add to Bulid Path to build it into the project.
The import is successful as shown in the figure:
During the project development process, you can refer to the docs folder (help document) to find index.html to open it. Click Quick start to learn dom4j and parse xml through the help document.
As shown in the figure:
Below I will introduce Java operation XML file with detailed examples, the file name is exmple.java.
package vastsum;import java.io.File;import java.util.Iterator;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.Node;import org.dom4j.io.SAXReader;/** * Read the xml document and obtain the document object. * This article is the first article of xml serialization. The following code can be run directly, and the source code download address is attached at the end. */class exple { public static void main(String[] args) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new File("./src/contact.xml")); /** * Operation method of node object*/ //Get document root root = document.getRootElement(); //Output the root label name System.out.println(root.getName()); //Get all child nodes under the root node (child nodes that do not cover child nodes) List<Element> list = root.elements() ; //The method of traversing List for (Element e:list){ System.out.println(e.getName()); } //Get the child nodes under the specified node Element contactElem = root.element("contact"); //First of all, you need to know the node you want to operate. List<Element> contactList = contactElem.elements(); for (Element e:contactList){ System.out.println(e.getName()); } //Calend the recursive function below to get the child node. getChildNodes(root); //Get the first sub-tag of the specified name under the current tag Element conElem = root.element("contact"); System.out.println(conElem.getName()); //Get deeper labels (get layer by layer) Element nameElem = root.element("contact").element("name"); System.out.println(nameElem.getName()); } //Recursively query the node function and output the node name private static void getChildNodes(Element elem){ System.out.println(elem.getName()); Iterator<Node> it= elem.nodeIterator(); while (it.hasNext()){ Node node = it.next(); if (node instanceof Element){ Element e1 = (Element)node; getChildNodes(e1); } } } }The following is the corresponding xml file, the file name is contact.xml:
<?xml version="1.0" encoding="utf-8"?><contactList> <contact id="001"> <name>Zhang San</name> <age>20</age> <phone>134222223333</phone> <email>[email protected]</email> <qq>432221111</qq> </contact> <contact id="002"> <name>Li Si</name> <age>20</age> <phone>134222225555</phone> <email>[email protected]</email> <qq>4322222222</qq> </contact> <contactTwo> <name>Wang Wu</name> <age>32</age> <phone>465431341</phone> <emali>[email protected]</emali> <qq>46164694</qq> </contactTwo> <test>Test</test> <test>Other uses</test></contactList>
The directory of the two files is shown in the title:
The above is the full content of the Java method to obtain XML nodes and read XML document nodes that the editor brings to you. I hope everyone will support Wulin.com more~