Xpath 入门教程

Posted zhou-test

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Xpath 入门教程相关的知识,希望对你有一定的参考价值。

准备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>

 

创建测试类

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 XMLUtil {
    public static void main(String[] args) throws ParserConfigurationException,SAXException, IOException, XPathExpressionException {
     
      DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document document = builder.parse(new File("D:\workspace_j2ee\YN_XmlParse4.0_20180712\src\com\resources\DBConfig.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("===========================================================");
     }

}

 

获取测试结果

Learning XML
===========================================================
en
===========================================================
book
===========================================================
COOKING
CHILDREN
WEB
===========================================================

 

以上是关于Xpath 入门教程的主要内容,如果未能解决你的问题,请参考以下文章

VIM 代码片段插件 ultisnips 使用教程

Python爬虫教程-21-xpath

markdown 打字稿...编码说明,提示,作弊,指南,代码片段和教程文章

python爬虫入门

推荐net开发cad入门阅读代码片段

使用 xpath 同时选择属性和内容?