Dom4j (XPath) parsing XML example

Source: Internet
Author: User
Tags xml example

Import java. Io. file;
Import java. util. List;
Import org. dom4j. Document;
Import org. dom4j. extends entexception;
Import org. dom4j. element;
Import org. dom4j. Io. saxreader;

Public class main {
Public static void main (string [] ARGs ){
Saxreader = NULL;
Document Doc = NULL;
Try {
Saxreader = new saxreader ();
Doc = saxreader. Read (new file ("D:/SS. xml "));
List <element> List = Doc. selectnodes ("// result // value ");
System. out. println (list + "list ");
For (Element EntryItem: list ){
System. out. println (EntryItem. getName ());
String table = EntryItem. elementTextTrim ("table ");
String SQL = EntryItem. elementTextTrim ("SQL ");
System. out. println ("table =" + EntryItem. elementTextTrim ("table "));
System. out. println ("SQL =" + EntryItem. elementTextTrim ("SQL "));

}
} Catch (exception e ){
E. printstacktrace ();
}
}
}

The running result is:

[Org. dom4j. tree. defaultelement @ 18f6235 [element: <value attributes: [Org. dom4j. tree. defaultattribute @ 1dfc547 [attribute: name value "value"]/>] list
Value
Table = tbl_notice
SQL = select title, content from tbl_notice order by ID DESC
 

 

The content of the SS. xml file is:

<? XML version = "1.0" encoding = "UTF-8"?>

<Result>
<Value name = "value">
<Table> tbl_notice </table>
<SQL> select title, content from tbl_notice order by id desc </SQL>
<Style> </style>
</Value>
</Result>

 

Jaxen-xx.xx.jar required

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.