xpath提取xml文档数据

xpath提取xml文档数据
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cn.hmm.day5;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.dom4j.Document;  
  6. import org.dom4j.DocumentException;  
  7. import org.dom4j.io.SAXReader;  
  8. import org.junit.Test;  
  9.   
  10. //应用xpath提取xml文档数据  
  11. public class Dwmo1 {  
  12.     @Test  
  13.     public void test1() throws DocumentException{  
  14.         SAXReader reader = new SAXReader();  
  15.         Document document = reader.read(new File("src/book.xml"));  
  16.           
  17.         String value =  document.selectSingleNode("//作者").getText();  
  18.         System.out.println(value);  
  19.     }  
  20. }  

uesr.xml:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <users>  
  3.     <user id="1" username="aaa" password="123" email="aa@sina.com" />  
  4.     <user id="2" username="bbb" password="123" email="bb@sina.com" />  
  5. </users>  


验证账号密码是存在xml中:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cn.hmm.day5;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.dom4j.Document;  
  6. import org.dom4j.DocumentException;  
  7. import org.dom4j.Node;  
  8. import org.dom4j.io.SAXReader;  
  9. import org.junit.Test;  
  10.   
  11. public class Demo2 {  
  12.   
  13.     @Test  
  14.     public void check() throws DocumentException{  
  15.         String username = "bbb";  
  16.         String password = "123";  
  17.           
  18.         SAXReader reader = new SAXReader();  
  19.         Document document = reader.read(new File("src/user.xml"));  
  20.           
  21.         Node node = document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']");  
  22.         if(node==null){  
  23.             System.out.println("对不起,密码错误或者用户名不存在!");  
  24.         }  
  25.         else{  
  26.             System.out.println("欢迎你,"+username);  
  27.         }  
  28.     }  
  29. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值