dom4j --用xpath排序xml

来源:互联网 发布:黑客摄像头监控软件 编辑:程序博客网 时间:2024/04/30 15:36

问题描述:

      在dom4j中可以通过xpath过滤xml数据中的节点,并且可以进行排序。调用方法有两种:

     

      方法一:org.dom4j.XPath   

      selectNodes(Object context, XPath sortXPath) 
           selectNodes evaluates the XPath expression on the given Nodeor Listof Nodes and returns the result as a List of Node s sorted by the sort XPath expression.

   

      方法二:org.dom4j.Node

      selectNodes(String xpathExpression, String comparisonXPathExpression) 
           selectNodes evaluates an XPath expression then sorts the results using a secondary XPath expression Returns a sorted List of Node instances.

 

解决方法:

      扩展DefaultXPath自己实现了一个NumberXPath类,源码如下:

      

Java代码 复制代码 收藏代码
  1. package org.dom4j.xpath;   
  2.   
  3. import org.dom4j.Node;   
  4.   
  5. public class NumberXPath extends DefaultXPath{   
  6.     public NumberXPath(String text){   
  7.         super(text);   
  8.     }   
  9.     protected Object getCompareNumberValue(Node node) {   
  10.         return numberValueOf(node);   
  11.     }   
  12. }  
[java] view plaincopy
  1. package org.dom4j.xpath;  
  2.   
  3. import org.dom4j.Node;  
  4.   
  5. public class NumberXPath extends DefaultXPath{  
  6.     public NumberXPath(String text){  
  7.         super(text);  
  8.     }  
  9.     protected Object getCompareNumberValue(Node node) {  
  10.         return numberValueOf(node);  
  11.     }  
  12. }  

    调用方法:

   

Java代码 复制代码 收藏代码
  1. String srcXML = "...xml文字...";   //参见xml文件   
  2. Document doc = DocumentHelper.parseText(srcXML);   
  3. List list = doc.selectNodes("doc/person/adds/add[@ID]");   
  4.   
  5. org.dom4j.XPath path =new NumberXPath("@ID");   
  6. path.sort(list);   
  7.   
  8. //查看结果   
  9. Iterator i1 = list.iterator();   
  10. while(i1.hasNext()){   
  11.     Element element = (Element) i1.next();   
  12.     System.out.println(element.attributeValue("ID"));   
  13. }  
[java] view plaincopy
  1. String srcXML = "...xml文字...";   //参见xml文件  
  2. Document doc = DocumentHelper.parseText(srcXML);  
  3. List list = doc.selectNodes("doc/person/adds/add[@ID]");  
  4.   
  5. org.dom4j.XPath path =new NumberXPath("@ID");  
  6. path.sort(list);  
  7.   
  8. //查看结果  
  9. Iterator i1 = list.iterator();  
  10. while(i1.hasNext()){  
  11.     Element element = (Element) i1.next();  
  12.     System.out.println(element.attributeValue("ID"));  
  13. }  

 

   XML文件:

   

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <doc>  
  3.     <person>  
  4.         <name>某人</name>  
  5.         <adds>               
  6.             <add ID="01">  
  7.                 <BS>10002</BS>  
  8.                 <note>西安市太白路</note>  
  9.             </add>  
  10.             <add ID="02">  
  11.                 <BS>10002</BS>  
  12.                 <note>空ID节点啊</note>  
  13.             </add>  
  14.             <add ID="12">  
  15.                 <BS>10002</BS>  
  16.                 <note>空ID节点啊</note>  
  17.             </add>  
  18.             <add>  
  19.     <BS xmlns="10001"/>  
  20.                 <note>西安市太白路2</note>  
  21.             </add>  
  22.          </adds>  
  23.     </person>  
  24. </doc>  
[xml] view plaincopy
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <doc>  
  3.     <person>  
  4.         <name>某人</name>  
  5.         <adds>              
  6.             <add ID="01">  
  7.                 <BS>10002</BS>  
  8.                 <note>西安市太白路</note>  
  9.             </add>  
  10.             <add ID="02">  
  11.                 <BS>10002</BS>  
  12.                 <note>空ID节点啊</note>  
  13.             </add>  
  14.             <add ID="12">  
  15.                 <BS>10002</BS>  
  16.                 <note>空ID节点啊</note>  
  17.             </add>  
  18.             <add>  
  19.     <BS xmlns="10001"/>  
  20.                 <note>西安市太白路2</note>  
  21.             </add>  
  22.          </adds>  
  23.     </person>  
  24. </doc>  

  



锁定Node.selectNodes(String arg1,String arg2);
arg1:xpath  语言,用来订位某个节点。
arg2:查到api是这么说的:is the XPath expression used to compare the results by for sorting
例如我要为A节点的m属性排序,则写法为:@m
对于降序,我从网上找来了方法:具体为:
File xmlFile = new File("...user.xml"); 
SAXReader reader = new SAXReader(); 
Document doc = reader.read(xmlFile); 
List<Element> nodes = doc.selectNodes("//user", "@id"); //升序 
for(Element e : nodes) { 
System.out.println("id : " + e.attributeValue("id")); 
System.out.println("name : " + e.attributeValue("name")); 

System.out.println("----------------------------------"); 
Collections.reverse(nodes); //降序 
for(Element e : nodes) { 
System.out.println("id : " + e.attributeValue("id")); 
System.out.println("name : " + e.attributeValue("name"));
 
使用了Collections对象
0 0