• xPath用法


    XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力,下面是一小示例,需要的朋友可以参考下
     
     

    XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言。XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力。起初 XPath 的提出的初衷是将其作为一个通用的、介于XPointer与XSL间的语法模型。但是 XPath 很快的被开发者采用来当作小型查询语言。

    XPathTest.java

    package com.hongyuan.test;
     
    
    import java.io.File;
    import java.io.IOException;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    public class XPathTest {
    
     public static void main(String[] args) throws ParserConfigurationException,
       SAXException, IOException, XPathExpressionException {
    
      // 解析文件,生成document对象
      DocumentBuilder builder = DocumentBuilderFactory.newInstance()
        .newDocumentBuilder();
      Document document = builder.parse(new File("bookstore.xml"));
    
      // 生成XPath对象
      XPath xpath = XPathFactory.newInstance().newXPath();
    
      // 获取节点值
      String webTitle = (String) xpath.evaluate(
        "/bookstore/book[@category='WEB']/title/text()", document,
        XPathConstants.STRING);
      System.out.println(webTitle);
    
      System.out.println("===========================================================");
    
      // 获取节点属性值
      String webTitleLang = (String) xpath.evaluate(
        "/bookstore/book[@category='WEB']/title/@lang", document,
        XPathConstants.STRING);
      System.out.println(webTitleLang);
    
      System.out.println("===========================================================");
    
      // 获取节点对象
      Node bookWeb = (Node) xpath.evaluate(
        "/bookstore/book[@category='WEB']", document,
        XPathConstants.NODE);
      System.out.println(bookWeb.getNodeName());
    
      System.out.println("===========================================================");
    
      // 获取节点集合
      NodeList books = (NodeList) xpath.evaluate("/bookstore/book", document,
        XPathConstants.NODESET);
      for (int i = 0; i < books.getLength(); i++) {
       Node book = books.item(i);
       System.out.println(xpath.evaluate("@category", book,
         XPathConstants.STRING));
      }
    
      System.out.println("===========================================================");
     }
    
    }
    
     
    
    bookstore.xml
    
    复制代码代码如下:
    
    <?xml version="1.0" encoding="utf-8" ?>
    <bookstore>
     <book category="COOKING">
       <title lang="en">Everyday Italian</title> 
       <author>Giada De Laurentiis</author> 
       <year>2005</year> 
       <price>30.00</price> 
     </book>
     <book category="CHILDREN">
       <title lang="en">Harry Potter</title> 
       <author>J K. Rowling</author> 
       <year>2005</year> 
       <price>29.99</price> 
     </book>
     <book category="WEB">
       <title lang="en">Learning XML</title> 
       <author>Erik T. Ray</author> 
       <year>2003</year> 
       <price>39.95</price> 
     </book>
    </bookstore>
    

      



    InputStream inputStream = new ByteArrayInputStream(responseXML.trim().getBytes("UTF-8"));
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(inputStream);
    XPath xpath = XPathFactory.newInstance().newXPath();
    setResponseCode((xpath.evaluate("/InterBOSS/SvcCont/AdditionRsp/Status", doc)));
    String ordNum = (xpath.evaluate("/InterBOSS/SvcCont/AdditionRsp/OperSeqList/OperSeq", doc));
    setResponseContent((xpath.evaluate("/InterBOSS/SvcCont/AdditionRsp/ErrDesc", doc)));

    运行效果
  • 相关阅读:
    LeetCode Array Easy 1. Two Sum
    关于VS2015 发布.net mvc 网站失败的问题
    2016计蒜之道复赛 百度地图的实时路况 floyd+cdq分治
    2016计蒜之道复赛 菜鸟物流的运输网络 网络流EK
    HDU5715 XOR 游戏 二分+字典树+dp
    HDU5697 刷题计划 dp+最小乘积生成树
    codeforces 687D Dividing Kingdom II 带权并查集(dsu)
    codeforces 687C
    codeforces 687B
    HDU 5693 D Game 区间dp
  • 原文地址:https://www.cnblogs.com/zengda/p/4765854.html
Copyright © 2020-2023  润新知