Example 2 of parsing XML using dom4j and xpath

Source: Internet
Author: User

It contains three files: studentinfo. XML (XML file to be parsed), dom4jreadexmple. java (main parsing class), testdom4jreadexmple. java (test parsing result) (because the code pasting tool that comes with the csdn blog will add some "... "characters, although easy to view, but not conducive to the Code copy out to run, and personally run the code is very important for programmers, therefore, the csdn blog's built-in code pasting tool is not used to insert code, but is used in a straightforward way ):

 Studentinfo. xml

<? XML version = "1.0" encoding = "gb2312"?>
<Students>
<Student age = "25"> <! -- If no age property exists, the default value is 20 -->
<Name> Cui weibing </Name>
<College> PC college </College>
<Telephone> 62354666 </telephone>
<Notes> male, born in 1982, with a master's degree, now enrolled at Beijing University of Posts and Telecommunications </Notes>
</Student>
<Student>
<Name> CWB </Name>
<College leader = "college leader"> PC college </College> <! -- If the leader attribute is not available, the default value is leader -->
<Telephone> 62358888 </telephone>
<Notes> male, born in 1987, with a master's degree, now enrolled at China Agricultural University </Notes>
</Student>
<Student age = "45">
<Name> XXXXX </Name>
<College leader = ""> XXX college </College>
<Telephone> 66666666 </telephone>
<Notes> watching, commenting </Notes>
</Student>
<Student age = "">
<Name> LXX </Name>
<College> yyyy college </College>
<Telephone> 88888888 </telephone>
<Notes> watching 111, commenting 222 </Notes>
</Student>
</Students>

Dom4jreadexmple. Java

Package dom4jexample. read;

Import java. Io. file;
Import java. util. hashmap;
Import java. util. iterator;
Import java. util. List;

Import org. dom4j. Attribute;
Import org. dom4j. Document;
Import org. dom4j. element;
Import org. dom4j. Io. saxreader;
/**
* XML programming using dom4j and xpath
* @ Author cuiweibo
* @ Since 2007.8.10
*/
Public class dom4jreadexmple {
/**
* Use XPath to operate the XML file, obtain the value of the specified node or attribute, and put it in hashmap.
* @ Param filename string the XML file to be operated (relative or absolute path)
* @ Param HM hashmap stores the selected result. Format: <nodename, nodevalue> or <nodename + attrname, attrvalue>
*/
Public void getselectednodevalue (string filename, hashmap <string, string> Hm ){
Try {
Saxreader = new saxreader ();
Document document = saxreader. Read (new file (filename ));
// Obtain the age when the student name is "Cui weibing"
List list = Document. selectnodes ("/students/student [name =/" Cui weibing/"]/@ age ");
Iterator iter = List. iterator ();
If (ITER. hasnext ()){
Attribute attribute = (attribute) ITER. Next ();
Hm. Put ("Cui weibing-" + attribute. getname (), attribute. getvalue ());
} Else {
Hm. Put ("Cui weibing-Age", "20 ");
}
// Obtain the age when the student name is "Cui weibing"
List = Document. selectnodes ("/students/student [name =/" CWB/"]/@ age ");
Iter = List. iterator ();
If (ITER. hasnext ()){
Attribute attribute = (attribute) ITER. Next ();
Hm. Put ("CWB-" + attribute. getname (), attribute. getvalue ());
} Else {
Hm. Put ("CWB-Age", "20 ");
}
// Obtain the name of the school where the student's name is "CWB"
List = Document. selectnodes ("/students/student [name =/" CWB/"]/College ");
Iter = List. iterator ();
If (ITER. hasnext ()){
Element element = (element) ITER. Next ();
String name = element. getname ();
String value = element. gettext ();
Hm. Put ("CWB-" + name, value );
}
// Obtain the Student name as the "CWB" school leader
List = Document. selectnodes ("/students/student [name =/" CWB/"]/College/@ leader ");
Iter = List. iterator ();
If (ITER. hasnext ()){
Attribute attribute = (attribute) ITER. Next ();
Hm. Put ("CWB-college-" + attribute. getname (), attribute. getvalue ());
} Else {
Hm. Put ("CWB-college-leader", "Leader ");
}
// Obtain the name of the school where the student name is "LXX"
List = Document. selectnodes ("/students/student [name =/" LXX/"]/College ");
Iter = List. iterator ();
If (ITER. hasnext ()){
Element element = (element) ITER. Next ();
String name = element. getname ();
String value = element. gettext ();
Hm. Put ("LXX-" + name, value );
}
// Obtain the leader of the school whose Student name is "LXX"
List = Document. selectnodes ("/students/student [name =/" LXX/"]/College/@ leader ");
Iter = List. iterator ();
If (ITER. hasnext ()){
Attribute attribute = (attribute) ITER. Next ();
Hm. Put ("LXX-college-" + attribute. getname (), attribute. getvalue ());
} Else {
Hm. Put ("LXX-college-leader", "Leader ");
}
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}

Testdom4jreadexmple. Java

Package dom4jexample. read;

Import java. util. hashmap;

/**
* Test dom4jreadexmple Parsing
* @ Author cuiweibo
* @ Since 2007.8.10
*/
Public class testdom4jreadexmple {
Public static void main (string [] ARGs ){
Try {
// Obtain the parsed Information
Hashmap <string, string> hashmap;
Dom4jreadexmple DRB = new dom4jreadexmple ();
// Use XPath to operate the XML file and obtain the desired Attribute Value
Hashmap = new hashmap <string, string> ();
DRB. getselectednodevalue ("studentinfo. xml", hashmap );
System. Out. println ("Cui weibing-age:" + hashmap. Get ("Cui weibing-Age "));
System. Out. println ("CWB-age:" + hashmap. Get ("CWB-Age "));
System. Out. println ("CWB-College:" + hashmap. Get ("CWB-College "));
System. Out. println ("CWB-college-leader:" + hashmap. Get ("CWB-college-Leader "));
System. Out. println ("LXX-College:" + hashmap. Get ("LXX-College "));
System. Out. println ("LXX-college-leader:" + hashmap. Get ("LXX-college-Leader "));
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}

Running result

Cui weibing-age: 25
CWB-age: 20
CWB-College: PC College
CWB-college-leader: school leader
LXX-College: yyyy College
LXX-college-leader: Leader

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.