dom4j xml 排序

问题描述:

      在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. }  
package org.dom4j.xpath;

import org.dom4j.Node;

public class NumberXPath extends DefaultXPath{
    public NumberXPath(String text){
        super(text);
    }
    protected Object getCompareNumberValue(Node node) {
        return numberValueOf(node);
    }
}

    调用方法:

   

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. }  
String srcXML = "...xml文字...";   //参见xml文件
Document doc = DocumentHelper.parseText(srcXML);
List list = doc.selectNodes("doc/person/adds/add[@ID]");

org.dom4j.XPath path =new NumberXPath("@ID");
path.sort(list);

//查看结果
Iterator i1 = list.iterator();
while(i1.hasNext()){
    Element element = (Element) i1.next();
    System.out.println(element.attributeValue("ID"));
}

 

   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>  

  



锁定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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值