JDOM中XPath.selectNodes()和XPath.selectSingNodes()用法

来源:互联网 发布:百度推广数据分析报告 编辑:程序博客网 时间:2024/05/16 14:51
JDOM中XPath.selectNodes()是返回一个节点集;而XPath.selectSingleNode()是返回符合要求的第一条记录。

1.测试用的message.xml文件:
Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <root>   
  3.     <person id="1">   
  4.         <username ip="11111">xiaoma</username>   
  5.         <password>xiaoma</password>   
  6.     </person>   
  7.     <person id="2">   
  8.         <username ip="22222">manager</username>   
  9.         <password>password2</password>   
  10.     </person>   
  11.     <person id="3">   
  12.         <username ip="33333">manager</username>   
  13.         <password>3333</password>   
  14.     </person>   
  15.     <hello id="999">   
  16.         <username ip="22444">hello</username>   
  17.         <password>world</password>   
  18.     </hello>   
  19.        
  20. </root>  

2.selectNodes()方法:
Java代码
  1. public static void list() throws JDOMException, IOException {   
  2.         SAXBuilder builder = new SAXBuilder();   
  3.         String xmlPath = "./src/xmldom/message.xml";   
  4.         // 获得文档对象   
  5.         Document document = builder.build(xmlPath);   
  6.         // 获得根节点   
  7.         Element root = document.getRootElement();   
  8.         List reslist = XPath.selectNodes(root, "/root/person");   
  9.         System.out.println("size:"+reslist.size());   
  10.         for(int i=0;i<reslist.size();i++){   
  11.             Element e = (Element)list.get(i);   
  12.             System.out.println("id:"+e.getAttributeValue("id"));   
  13.             System.out.println("username:"+e.getChild("username").getText());   
  14.             System.out.println("ip:"+e.getChild("username").getAttributeValue("ip"));   
  15.             System.out.println("password:"+e.getChild("password").getText());   
  16.             System.out.println("-------------------");   
  17.         }   
  18.            
  19.     }  

运行结果:
Java代码
  1. size:3  
  2. id:1  
  3. username:xiaoma   
  4. ip:11111  
  5. password:xiaoma   
  6. -------------------   
  7. id:2  
  8. username:manager   
  9. ip:22222  
  10. password:password2   
  11. -------------------   
  12. id:3  
  13. username:manager   
  14. ip:33333  
  15. password:3333  
  16. -------------------  

3.selectSingleNode()方法:
Java代码
  1. Element e1 = (Element)XPath.selectSingleNode(root, "/root/hello/username");   
  2. System.out.println("ip:"+e1.getAttributeValue("ip"));   
  3. System.out.println("username:"+e1.getText());   
  4. System.out.println("--------------");   
  5. Element e2 = (Element)XPath.selectSingleNode(root, "/root/hello");   
  6. System.out.println("id:"+e2.getAttributeValue("id"));   
  7. System.out.println("ip:"+e2.getChild("username").getAttributeValue("ip"));   
  8. System.out.println("username:"+e2.getChild("username").getText());   
  9. System.out.println("password:"+e2.getChild("password").getText());  

测试结果:
Java代码
  1. ip:22444  
  2. username:hello   
  3. --------------   
  4. id:999  
  5. ip:22444  
  6. username:hello   
  7. password:world  
原创粉丝点击