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 ~