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);
        }
    }

  • 相关阅读:
    CentOS 8配置Java环境
    记录一个免费的开源API接口管理工具
    WebAPI 查询lookup字段的属性
    Windows环境变量配置与读取
    The specified Active Directory user already exists as a Dynamics 365 user
    QueryExpression之GreaterEqual和LessEqual
    Dynamics CRM Plugin Use Config
    【转】Reports SPN/SSPI Problems
    【转】Report Server cannot load the TERADATA / SQLPDW extension
    iOS 自动布局
  • 原文地址:https://www.cnblogs.com/aasu/p/9170626.html
Copyright © 2011-2022 走看看