XPath 小案例

1 <?xml version="1.0" encoding="UTF-8"?>
2 <users>
3     <user id="1" username="aaa" password="123" email="123@163.com"/>
4     <user id="2" username="bbb" password="456" email="678@163.com"/>
5 </users>
 1 package dom4j;
 2 
 3 import java.io.File;
 4 
 5 import org.dom4j.Document;
 6 import org.dom4j.Node;
 7 import org.dom4j.io.SAXReader;
 8 
 9 public class demo1 {
10     
11     public static void main(String[] args) throws Exception 
12     {
13         String username="aaa";
14         String password="124";
15         
16         //检测xml文档中是否有匹配的用户名和密码
17         SAXReader reader = new SAXReader();
18         Document document = reader.read(new File("src/dom4j/system.xml"));
19         Node node=document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']");
20         
21         if(node==null)
22         {
23             System.out.println("用户名或密码错误!!");
24         }
25         else
26         {
27             System.out.println("登录成功!!");
28         }
29     }
30 
31 }

XPath要注意使用帮助手册,特别简单,手册地址:http://www.zvon.org/xxl/XPathTutorial/General/examples.html

posted on 2015-02-04 23:29  曾经的码农  阅读(170)  评论(0编辑  收藏  举报