A single child node object is obtained under the parent node.
Element memberelm=root.element ("member"); "Member" is a section roll call
Gets the parent node to traverse all child nodes named "member". List nodes = rootelm.elements ("member");
for (Iterator it = Nodes.iterator (); It.hasnext ();) {
Element elm = (Element) It.next ();
Do something
}
Iterate through all the properties of a node
Element root=document.getrootelement ();
For (iterator It=root.attributeiterator (); It.hasnext ();) {
attribute attribute = (attribute) it.next ();
String Text=attribute.gettext ();
System.out.println (text);
}
XML file: XML version = "1.0" encoding = "GBK"?>
< doc >
< Person id = "1" sex = "M" >
< name >zhangsan </name >
< age >32 </age >
< adds >
< add code = "Home" >home add </Add >
< add code = "com" >com add </Add >
</adds >
</person >
< Person id = "2" sex = "W" >
< name >lisi </name >
< age >22 </age >
< adds >
< add ID = "" id = "" "Code =" Home ">home add </Add >
< add id = "" "id =" "code =" com ">com add </Add >
< add id = "in" code = "com" >com add </Add >
</adds >
</person >
</doc >
Parsing code: Package com.topsoft.test;
Import Org.dom4j.io.SAXReader;
Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import org.dom4j.Element;
Import Org.dom4j.Node;
Import Java.util.Iterator;
Import java.util.List;
Import Java.io.InputStream;
/**
* Created by IntelliJ idea.<br>
* <B>USER</B>: leizhimin<br>
* <B>DATE</B>: 2008-3-26 15:53:51<br>
* <b>note</b>: DOM4J traversal parse XML test
*/
public class Testdom4j {
/**
* Gets the document object for the specified XML document that the XML file must be found in the Classpath
*
* @param xmlfilepath XML file path
* @return Document Object
*/
public static Document parse2document (String xmlfilepath) {
Saxreader reader = new Saxreader ();
Document document = NULL;
try {
InputStream in = testdom4j. Class.getresourceasstream (Xmlfilepath);
Document = Reader.read (in);
catch (Documentexception e) {
System.out.println (E.getmessage ());
System.out.println ("read classpath XMLfileName file has an exception, check classpath and file name exists.") ");
E.printstacktrace ();
}
return document;
}
public static void Testparsexmldata (String xmlfilename) {
Produces a parser object
Saxreader reader = new Saxreader ();
To convert an XML document to an object of document
Document document = Parse2document (XMLfileName);
Get the root element of a document
Element root = Document.getrootelement ();
Defines a buffer string object that saves output XML data
StringBuffer sb = new StringBuffer ();
Sb.append ("Parse XML through dom4j and output data: \ n");
Sb.append (XMLfileName + "\ n");
Sb.append ("----------------traverse start----------------\ n");
Iterate through the child elements of the current element (this is the root element)
for (Iterator I_pe = Root.elementiterator (); I_pe.hasnext ();) {
Element E_pe = (Element) I_pe.next ();
Gets the name of the current element
String person = e_pe.getname ();
Gets the value of the current element's ID and sex property and assigns it to the Id,sex variable respectively
String id = e_pe.attributevalue ("id");
String sex = e_pe.attributevalue ("Sex");
String name = e_pe.element ("name"). GetText ();
String age = e_pe.element (' age '). GetText ();
Storing data in a buffer string object
Sb.append (person + ": \ n");
Sb.append ("\tid=" + ID + "sex=" + sex + "\ n");
Sb.append ("T" + "name=" + name + "age=" + Age + "\ n");
Gets the child element under the current element E_pe (this is the person element) adds
Element E_adds = e_pe.element ("adds");
Sb.append ("\ T" + e_adds.getname () + "\ n");
Traverses the child elements of the current element E_adds (here is the adds element)
for (Iterator I_adds = E_adds.elementiterator (); I_adds.hasnext ();) {
Element E_add = (Element) I_adds.next ();
String code = e_add.attributevalue ("code");
String add = E_add.gettexttrim ();
Sb.append ("\t\t" + e_add.getname () + ":" + "code=" + code + "Value=\" "+ add +" \ \ n ");
}
Sb.append ("\ n");
}
Sb.append ("-----------------traversal end-----------------\ n");
System.out.println (Sb.tostring ());
system.out.println ("---------get an element through XPath----------");