Java-xpath Learning

Source: Internet
Author: User
Tags gettext xpath

/**

    • Use XPath technology to get content under any level in an XML file DOM4J-based XPath technology
    • 1) Ability to quickly locate required elements in an XML file without navigating from one of the root elements to the desired child elements
    • Document.selectnodes (): Get all XPath-compliant elements
    • Document.selectsinglenode (): Gets the first element of all elements that conform to the XPath format
    • Node type is element/text/attribute/document/... The parent interface of the type
    • */
Import Java.io.file;import java.util.list;import Java.util.scanner;import Org.dom4j.document;import Org.dom4j.documentexception;import Org.dom4j.element;import Org.dom4j.io.saxcontenthandler;import Org.dom4j.io.saxreader;import org.junit.test;/** * Use XPath technology to get content under any level in the XML file based on dom4j XPath technology * 1) ability to quickly locate the required element in an XML file, No need to navigate from one to the root element to the desired child element * Document.selectnodes (): Get all XPath-compliant elements * Document.selectsinglenode () : Gets the first element of all XPath-compliant elements * node type is element/text/attribute/document/... Parent interface of type * */public class Xpath {@Test public void xpathtest () throws Exception {Saxreader reader = new Saxre        Ader ();        Document document = Reader.read (new File ("Src/day2/domx/car.xml"));        String XPath = "//Unit Price";        list<element> elementlist = Document.selectnodes (XPath);            for (Element e:elementlist) {System.out.println (E.gettext ());        System.out.println ("================="); }} @Test public void Xpathtest1 () throws Exception {Saxreader reader = new Saxreader ();        Document document = Reader.read (new File ("Src/day2/domx/car.xml"));        String XPath = "//Unit Price";        list<element> elementlist = Document.selectnodes (XPath);    System.out.println ("The price of the second car is:" + elementlist.get (1). GetText ());        } @Test public void Xpathtest2 () throws Exception {Saxreader reader = new Saxreader ();        Document document = Reader.read (new File ("Src/day2/domx/car.xml"));        String XPath = "//Unit Price";        element element = (Element) Document.selectsinglenode (XPath);    System.out.println ("The price of the first car is:" + element.gettext ()); } @Test public void login () throws Exception {//reads the user's input information on the keyboard Scanner Scanner = new Scanner (system.in        );        System.out.println ("User name:");        String username = Scanner.nextline ();        System.out.print ("Password:");        String password = scanner.nextline ();        System.out.println (username+ ":" +password); Parses the XML file and queries the specified element Saxreader SaxreaDer = new Saxreader ();        Document document = Saxreader.read (new File ("Src/day2/domx/users.xml"));        String XPath = "//user[@username = '" +username+ "' and @password = '" +password+ "']";        element element = (Element) Document.selectsinglenode (XPath);        if (element! = null) {System.out.println ("login succeeded");        }else {System.out.println ("Login Failed"); }    }}

User.xml

<?xml version="1.0" encoding="UTF-8"?><root>   <user id = "id001" username ="liwen" password="123456"></user>   <user id = "id002" username ="python" password="23456"></user></root>

Results:
User name:
Liwen
Password: 123456
Landing success

Java-xpath Learning

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.