欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何使用XPath提取xml文档数据

程序员文章站 2022-06-08 23:20:02
本文实例为大家分享了xpath提取xml文档数据具体代码,供大家参考,具体内容如下 import java.util.list; import org.d...

本文实例为大家分享了xpath提取xml文档数据具体代码,供大家参考,具体内容如下

import java.util.list;
 
import org.dom4j.document;
import org.dom4j.node;
import org.dom4j.io.saxreader;
import org.junit.test;
/*
 * 使用xpath查找xml文档数据
 * 
 */
public class demoxpath {
  @test
  //输出book.xml中所有price元素节点的文本值
  public void test1() throws exception {
    saxreader reader = new saxreader();
    document document = reader.read("src/main/java/book.xml");
    list<? extends node> selectnodes = document.selectnodes("//price");
    for(node node : selectnodes) {
      string text = node.gettext();
      system.out.println(text);
    }
  }
   
  @test
  //输出book.xml中第二本书的price元素节点的文本值
  public void test2() throws exception {
    saxreader reader = new saxreader();
    document document = reader.read("src/main/java/book.xml");
    node selectsinglenode = document.selectsinglenode("/bookshelf/book[2]/price");
    string text = selectsinglenode.gettext();
    system.out.println(text);
  }
   
  @test
  //输出book.xml中第二本书和第三本书的author元素节点的文本值
  public void test3() throws exception {
    saxreader reader = new saxreader();
    document document = reader.read("src/main/java/book.xml");
    list<? extends node> selectsinglenode = document.selectnodes("/bookshelf/book[position()>1]/author");
    for (node node : selectsinglenode) {
      string text = node.gettext();
      system.out.println(text);
    }
  }
   
  @test
  //输出book.xml中含有属性id的所有name的文本值
  public void test4() throws exception {
    saxreader reader = new saxreader();
    document document = reader.read("src/main/java/book.xml");
    list<? extends node> selectsinglenode = document.selectnodes("//name[@id]");
    for (node node : selectsinglenode) {
      string text = node.gettext();
      system.out.println(text);
    }
  }
   
  @test
  //输出book.xml中含有属性id="1111"的name的文本值
  public void test5() throws exception {
    saxreader reader = new saxreader();
    document document = reader.read("src/main/java/book.xml");
    node selectsinglenode = document.selectsinglenode("//name[@id=\"1111\"]");
    string text = selectsinglenode.gettext();
    system.out.println(text);
  }
   
  @test
  //输出book.xml中含有属性id="1112"的book的author的文本值
  public void test6() throws exception {
    saxreader reader = new saxreader();
    document document = reader.read("src/main/java/book.xml");
    node selectsinglenode = document.selectsinglenode("//book[name[@id=\"1112\"]]/author");
    string text = selectsinglenode.gettext();
    system.out.println(text);
  }
   
  @test
  //输出book.xml中第一本book的id的属性值
  public void test7() throws exception {
    saxreader reader = new saxreader();
    document document = reader.read("src/main/java/book.xml");
    node selectsinglenode = document.selectsinglenode("//book[1]/name");
    string text = selectsinglenode.valueof("attribute::id");//获取id属性
    system.out.println(text);
  }
   
  @test
  //输出book.xml中book的name的id的属性值为1112的对应的sn的属性值
  public void test8() throws exception {
    saxreader reader = new saxreader();
    document document = reader.read("src/main/java/book.xml");
    list<? extends node> selectnodes = document.selectnodes("//book/name");
    for (node node : selectnodes) {
      if(node.valueof("attribute::id").equals("1112")) {
        system.out.println(node.valueof("attribute::sn"));
      }
    }
  }
}
 <dependency>  
   <groupid>junit</groupid>  
   <artifactid>junit</artifactid>  
   <version>4.11</version>  
  </dependency>  
  <dependency>  
   <groupid>log4j</groupid>  
   <artifactid>log4j</artifactid>  
   <version>1.2.17</version>  
  </dependency>  
  <dependency>  
   <groupid>dom4j</groupid>  
   <artifactid>dom4j</artifactid>  
  </dependency>
<dependency>
 <groupid>jaxen</groupid>
 <artifactid>jaxen</artifactid>
 <version>1.1.6</version>
</dependency>
 <?xml version="1.0" encoding="utf-8"?>
 
<bookshelf>
 <book>
  <name id="1111" sn="sdd8">tomorrow</name> 
  <author>hiskell</author> 
  <price>$40</price>
 </book> 
 <book>
  <name id="1112" sn="sdd9">goodbye to you</name> 
  <author>giddle</author> 
  <price>$25</price>
 </book> 
 <book>
  <name id="1113" sn="sdd0">sea and old</name> 
  <author>heminw</author> 
  <price>$28</price>
 </book>
</bookshelf>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。