zoukankan      html  css  js  c++  java
  • dom4j支持Xpath的具体操作

    ***默认情况下,dom4j不支持xpath。

      如果想要使用xpath,需要引入jaxen-1.1-beta-6.jar包。

    在dom4j中提供了两个方法来支持xpath。

    ***selectNodes("xpath表达式")

        ---获取多个节点

    ***selectSingleNode("xpath表达式")

        ---获取一个节点

    **使用xpath实现:查询所有name元素的值

        所有name元素的值用://name  

    animal.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <animal>
      <cat id1="sususu">
        <name>汤姆</name>  
        <color>black</color>  
        <age>30</age>  
        <sex>男</sex>
      </cat>  
      <friend>丫丫</friend>  
      <cat>
        <name>丑小鸭</name>  
        <color>yellow</color>  
        <age>15</age>
      </cat>
    </animal>
    dom4jXpath.java

    package example4;

    import java.util.List;

    import org.dom4j.Document;
    import org.dom4j.Node;

    public class dom4jXpath {
        public static void main(String[] args) {
            //selectName();
            selectFirstName();
        }
        //使用xpath获取所有结点
        public static void selectName() {
            //得到document对象
            Document document=dom4jClass.getDocument();
            List<Node> list=document.selectNodes("//age");
            //遍历list集合
            for (Node node : list) {
                String s=node.getText();
            System.out.println(s);
            }
        }
        //使用xpath获取第一个cat下面的name的值
        public static void selectFirstName() {
            Document document=dom4jClass.getDocument();
            Node node=document.selectSingleNode("//cat[@id1='sususu']/name");
            String s=node.getText();
            System.out.println(s);
        }
    }

  • 相关阅读:
    小数化分数2
    Sum of divisors
    Subsequence
    Lowest Bit
    Specialized Four-Digit Numbers
    Hunters
    Pet
    测试你是否和LTC水平一样高
    Bank Interest
    bzoj 1295
  • 原文地址:https://www.cnblogs.com/aasu/p/9170626.html
Copyright © 2011-2022 走看看