Below I will bring you a simple example of using XPath in dom4j. Let me share it with you now and give it as a reference for everyone.
looks like this:
package com.wzh.test.xpath; import java.io.File; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.io.SAXReader; public class Demo4 { public static void main(String[] args) throws DocumentException { SAXReader reader=new SAXReader(); Document document=reader.read(new File("src/book.xml")); String value=document.selectSingleNode("//书名").getText(); System.out.println(value); //检测xml文档是否有匹配的用户名和密码 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("用户名密码错误"); } else { System.out.println("登录成功"); } } }
book.xml
<书架> <书> <书名>Java就业培训教材书名> <作者>张孝祥作者> <售价>39.00元售价> 书> <书> <书名>Java网页开发书名> <作者>张孝祥作者> <售价>29.00元售价> 书> 书架>
users. xml
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to use native ajax to process json strings
##Introduction to the 4 common request methods of ajax in jQuery
Let’s talk about our views and understanding of Ajax form submission
The above is the detailed content of Simple example of using XPath in dom4j. For more information, please follow other related articles on the PHP Chinese website!