Objectives:Parse an XML file and output all attributes and element values.
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 = "22"
Id = "23" code = "home"> Home add </Add>
<Add
Id = "23"
Id = "22" code = "com"> COM add </Add>
<Add
Id = "24"
Code = "com"> COM add </Add>
</Adds>
</Person>
</DOC>
Package com. topsoft. test;
Import org. dom4j. Io. saxreader;
Import org. dom4j. Document;
Import org. dom4j. extends entexception;
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>: 15:53:51 <br>
* <B> note </B>: dom4j traversal and parsing XML Test
*/
Public class testdom4j {
/**
* Get the Document Object of the specified XML document. The XML file must be available in 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 (incluentexception e ){
System. Out. println (E. getmessage ());
System. Out. println ("An error occurred while reading the xmlfilename file under classpath. Check whether the classpath and file name exist! ");
E. printstacktrace ();
}
Return document;
}
Public static
Void testparsexmldata (string xmlfilename ){
// Generate a parser object
Saxreader reader = new saxreader ();
// Convert an XML document to a Document Object
Document document = parse2document (xmlfilename );
// Obtain the root element of the document
Element root = Document. getrootelement ();
// Define a buffer String object for saving the 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 ");
// Traverse the child element of the current element (root element here)
For (iterator I _pe = root. elementiterator (); I _pe.hasnext ();){
Element e_pe = (element) I _pe.next ();
// Obtain the name of the current element
String person = e_pe.getname ();
// Obtain the ID and sex attribute values of the current element and assign them to the ID and sex variables 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 ();
// Store 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 ");
// Obtain the adds sub-element under the current element e_pe (here it is the person element)
Element e_adds = e_pe.element ("adds ");
SB. append ("\ t" + e_adds.getname () +
"\ N ");
// Traverse the child element of the current element e_adds (here it 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" + e_add.getname () +
":" + "Code =" + code +
"Value = \" "+ Add +
"\" \ N ");
}
SB. append ("\ n ");
}
SB. append ("----------------- traverse end ----------------- \ n ");
System. Out. println (sb. tostring ());
System. Out. println ("--------- get an element through XPath ----------");
Node node1 = Document. selectsinglenode ("/doc/person ");
System. Out. println ("output node:" +
"\ T" + node1.asxml ());
Node node2 = Document. selectsinglenode ("/doc/person/@ sex ");
System. Out. println ("output node:" +
"\ T" + node2.asxml ());
Node node3 = Document. selectsinglenode ("/doc/person [name = \" zhangsan \ "]/age ");
System. Out. println ("output node:" +
"\ T" + node3.asxml ());
System. Out. println ("\ n --------- test the list node obtained by XPath ------------");
List list = Document. selectnodes ("/doc/person [name = \" zhangsan \ "]/Adds/Add ");
For (iterator it = List. iterator (); it. hasnext ();){
Node nodex = (node) it. Next ();
System. Out. println (nodex. asxml ());
}
System. Out. println ("\ n --------- test of retrieving elements by ID ----------");
System. Out. println ("trap: Get by ID. The attribute name of the element ID must be" upper-case id ". The lower-case" ID "is considered as a common attribute! ");
String id22 = Document. elementbyid ("22"). asxml ();
String id23 = Document. elementbyid ("23"). asxml ();
String id24 = NULL;
If (document. elementbyid ("24 ")! =
Null ){
Id24 = Document. elementbyid ("24"). asxml ();
} Else {
Id24 = "null ";
}
System. Out. println ("id22 =" + id22 );
System. Out. println ("id23 =" + id23 );
System. Out. println ("id24 =" + id24 );
}
Public static
Void main (string ARGs []) {
Testparsexmldata ("/person. xml ");
}
}
Running result:Parse XML through dom4j and output data:
/Person. xml
---------------- Traverse start ----------------
Person:
Id = 1 sex = m
Name = zhangsan age = 32
Adds
Add: code = home value = "Home Add"
Add: code = com value = "COM Add"
Person:
Id = 2 sex = W
Name = Lisi age = 22
Adds
Add: code = home value = "Home Add"
Add: code = com value = "COM Add"
Add: code = com value = "COM Add"
----------------- Traverse end -----------------
--------- Get an element through XPath ----------
Output node: <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>
Output node: Sex = "M"
Output node: <age> 32 </age>
--------- Test of getting list nodes through XPath ------------
<Add
Code = "home"> Home add </Add>
<Add
Code = "com"> COM add </Add>
--------- Test for retrieving elements by ID ----------
Trap: Get by ID. The element ID attribute name must be "upper-case id". The lower-case "ID" will be considered as a normal attribute!
Id22 = <add
Id = "22"
Id = "23" code = "home"> Home add </Add>
Id23 = <add
Id = "23"
Id = "22" code = "com"> COM add </Add>
Id24 = NULL
Process finished with exit code 0