栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

import java.util.List;
 
import org.dom4j.document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;

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 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 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 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 selectNodes = document.selectNodes("//book/name");
    for (Node node : selectNodes) {
      if(node.valueOf("attribute::id").equals("1112")) {
 System.out.println(node.valueOf("attribute::sn"));
      }
    }
  }
}
   
   junit  
   junit  
   4.11  
    
    
   log4j  
   log4j  
   1.2.17  
    
    
   dom4j  
   dom4j  
  

 jaxen
 jaxen
 1.1.6
 
 

 
  Tomorrow 
  Hiskell 
  $40
  
 
  Goodbye to You 
  Giddle 
  $25
  
 
  Sea and Old 
  Heminw 
  $28
 

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/137128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号