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

  • 相关阅读:
    Linux TCP/IP 连接查看和问题解决
    Linux Tomcat 7.0 管理员登录时"401 Unauthorized" 问题解决方法
    Tomcat 性能监控工具jvisualvm, JConsole
    Tomcat 实战-调优方案
    Linux Tomcat8 访问管理页面 403 Access Denied
    docker redis
    Spring Boot 定时任务
    Nginx rewrite使用
    五 python 发送邮件
    关于注解继承性的一个例子
  • 原文地址:https://www.cnblogs.com/aasu/p/9170626.html
Copyright © 2011-2022 走看看