Simple example of using XPath in dom4j, dom4jxpath instance

Source: Internet
Author: User

Simple example of using XPath in dom4j, dom4jxpath instance

As follows:

Package com. wzh. test. xpath; import java. io. file; import org. dom4j. document; import org. dom4j. extends entexception; import org. dom4j. node; import org. dom4j. io. SAXReader; public class Demo4 {public static void main (String [] args) throws extends entexception {SAXReader reader = new SAXReader (); Document document = reader. read (new File ("src/book. xml "); String value = document. selectSingleNode ("// Title "). getText (); System. out. println (value); // check whether the xml document has a matched username and password String username = "aaa"; String password = "123"; reader = new SAXReader (); document = reader. read (new File ("src/users. xml "); Node node = document. selectSingleNode ("// user [@ username = '" + username + "'" + "and @ password = '" + password + "']"); if (node = null) {System. out. println ("Incorrect username and password");} else {System. out. println ("Logon successful ");}}}

Book. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Bookshelves> <book> <title> Java Employment Training Materials </title> <author> Zhang Xiaoxiang </author> <price> 39.00 RMB </price> </book> <book> <title> Java Web development </title> <author> Zhang Xiaoxiang </author> <price> 29.00 RMB </price> </book> </bookshelf>

Users. xml

<?xml version="1.0" encoding="UTF-8"?><users>  <user id="1" username="aaa" password="123" email="aa@sina.com"></user>  <user id="2" username="bbb" password="456" email="bb@sina.com"></user></users>

The above is all the content of the simple example of using XPath in dom4j provided by xiaobian. I hope it will be helpful to you and support more customers ~

Related Article

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.