Dom4j using XPath to navigate directly to a tag instance when parsing xml

Source: Internet
Author: User
Tags gettext xpath

It Programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary. package edu.dom4j.dom;

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;

Import Org.junit.Test;

public class Xpathreadxml {

Private String Xmlfile1 = "Webroot/product2.xml";

Private String Xmlfile2 = "Webroot/users.xml";

Public Document getdocument (String xmlfile) throws documentexception{

Saxreader reader = new Saxreader ();

Return Reader.read (xmlfile);

}

Read <product> bottom <price> text value

@Test

public void ReadText () throws documentexception{

Document document = GetDocument (Xmlfile1);

If the path is preceded by a slash/start, then the path represents the absolute path to an element

String xmlPath1 = "/catalog/product/price";

If the path begins with a double slash////, then all elements in the document that meet the double slash//after rule (regardless of hierarchy) are selected

String xmlPath2 = "//price";

List<element> pricelist = Document.selectnodes (xmlPath1);

for (Element price:pricelist) {

System.out.println ("<" +price.getname () + ">" +price.gettext () + "</" +price.getname () + ">");

}

System.out.println ("-------------------");

Element product1 = Document.getrootelement (). Element ("Product");

The effect of Product1 is the same as the document query.

Pricelist = Product1.selectnodes (xmlPath1);

for (Element price:pricelist) {

System.out.println ("<" +price.getname () + ">" +price.gettext () + "</" +price.getname () + ">");

}

System.out.println ("-------------------");

Element Price1 = (Element) Document.selectsinglenode (XMLPATH1);

if (price1!=null)

System.out.println ("<" +price1.getname () + ">" +price1.gettext () + "</" +price1.getname () + ">");

System.out.println ("-------------------");

Price1 = (Element) document.selectsinglenode (xmlPath2);

System.out.println ("<" +price1.getname () + ">" +price1.gettext () + "</" +price1.getname () + ">");

System.out.println ("-------------------");

Pricelist = Document.selectnodes (xmlPath2);

for (Element price:pricelist) {

System.out.println ("<" +price.getname () + ">" +price.gettext () + "</" +price.getname () + ">");

}

/**

* Output Result:

<price>80.0</price>

<price>100.0</price>

-------------------

<price>80.0</price>

<price>100.0</price>

-------------------

<price>80.0</price>

-------------------

<price>80.0</price>

-------------------

<price>80.0</price>

<price>100.0</price>

**/

}

Verify that the user is logged on successfully

@Test

public void Validelogin () throws documentexception{

Document document = GetDocument (Xmlfile2);

String username = "AAA";

String password = "Aaapass";

String Xmlpath = "//user[@name = '" "+username+" ' and @password = ' "+password+" '] ";

Node node = document.selectsinglenode (Xmlpath);

if (node!=null) {

System.out.println ("landed successfully. ");

}else {

System.out.println ("User name and password does not exist. ");

}

}

}

product2.xml File:

<?xml version= "1.0" encoding= "Utf-8"?>

<catalog id= "Cata1" >

<product category= "Handtool" inventory= "Instock" >

<specifications weight= "2.0kg" > Wrench </specifications>

<price street= "Hong Kong Street" >80.0</price>

<notes> This is the wrench </notes>

</product>

<product category= "Table" >

<specifications> Tables </specifications>

<price street= "Macau Street" wholesale= "part" >100.0</price>

</product>

</catalog>

users.xml File:

<?xml version= "1.0" encoding= "Utf-8"?>

<users>

<user name= "AAA" password= "Aaapass" ></user>

<user name= "BBB" password= "Bbbpass" ></user>

<user name= "CCC" password= "Cccpass" ></user>

<user name= "ddd" password= "Dddpass" ></user>

</users>


For the use of XPath positioning see : Xpathtutorial.chm (Download address: http://download.csdn.net/detail/xh16319/4587406)



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.