dom4j –用xpath排序xml

  • Post author:
  • Post category:其他


问题描述:

在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. import


    org.dom4j.Node;




  3. public




    class


    NumberXPath


    extends


    DefaultXPath{



  4. public


    NumberXPath(String text){



  5. super


    (text);

  6. }



  7. protected


    Object getCompareNumberValue(Node node) {



  8. return


    numberValueOf(node);

  9. }

  10. }


  1. package


    org.dom4j.xpath;




  2. import


    org.dom4j.Node;




  3. public




    class


    NumberXPath


    extends


    DefaultXPath{



  4. public


    NumberXPath(String text){



  5. super


    (text);


  6. }


  7. protected


    Object getCompareNumberValue(Node node) {



  8. return


    numberValueOf(node);


  9. }

  10. }

调用方法:

Java代码

复制代码


收藏代码



  1. String srcXML =


    “…xml文字…”


    ;


    //参见xml文件




  2. Document doc = DocumentHelper.parseText(srcXML);

  3. List list = doc.selectNodes(


    “doc/person/adds/add[@ID]”


    );


  4. org.dom4j.XPath path =


    new


    NumberXPath(


    “@ID”


    );

  5. path.sort(list);




  6. //查看结果



  7. Iterator i1 = list.iterator();



  8. while


    (i1.hasNext()){

  9. Element element = (Element) i1.next();

  10. System.out.println(element.attributeValue(


    “ID”


    ));

  11. }


  1. String srcXML =


    “…xml文字…”


    ;


    //参见xml文件




  2. Document doc = DocumentHelper.parseText(srcXML);

  3. List list = doc.selectNodes(

    “doc/person/adds/add[@ID]”


    );



  4. org.dom4j.XPath path =

    new


    NumberXPath(


    “@ID”


    );


  5. path.sort(list);



  6. //查看结果




  7. Iterator i1 = list.iterator();


  8. while


    (i1.hasNext()){


  9. Element element = (Element) i1.next();

  10. System.out.println(element.attributeValue(

    “ID”


    ));


  11. }

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


    >




  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对象



版权声明:本文为u013035080原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。