zoukankan      html  css  js  c++  java
  • 解析xpath获取到的标签

    一、book.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <bookstore>
        <book category="children">
            <title lang="en">Harry Potter</title>
            <author>J K. Rowling</author>
            <year>2005</year>
            <price>29.99</price>
        </book>
        <book category="cooking">
            <title lang="en">Everyday Italian</title>
            <author>Giada De Laurentiis</author>
            <year>2005</year>
            <price>30.00</price>
        </book>
        <book category="web">
            <title lang="en">Learning XML</title>
            <author>Erik T. Ray</author>
            <year>2003</year>
            <price>39.95</price>
        </book>
        <book category="web">
            <title lang="uk">XQuery Kick Start</title>
            <author>James McGovern</author>
            <year>2003</year>
            <price>49.99</price>
        </book>
    </bookstore>
    package edu.aeon.xml;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    /**
     * [说明]:xpath获取节点
     * @author aeon(qq:1584875179)
     *
     */
    public class XpathParser {
    
        public static void main(String[] args) {
            try {
                DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
                Document document=documentBuilder.parse("config/book.xml");
                XPath xPath= XPathFactory.newInstance().newXPath();
                /**
                 * 获取 bookstore 节点下 book 属性 category 值为 web 下的第二个 title 节点的文本内容
                 * xpath路径为:/bookstore/book[@category='web'][2]/title/text()
                 */
                String str1=(String) xPath.evaluate("/bookstore/book[@category='web'][2]/title/text()", document, XPathConstants.STRING);
                System.out.println(str1);
                /**
                 * 获取 bookstore 节点下 book 属性 category 值为 web 的 title 属性lang为en 的节点内容
                 * xpath路径为:/bookstore/book[@category='web']/title[@lang='en']/text()
                 */
                String str2=(String) xPath.evaluate("/bookstore/book[@category='web']/title[@lang='en']/text()", document, XPathConstants.STRING);
                System.out.println(str2);
                /**
                 * 获取bookstore下book属性category值为cooking的title的lang属性的值
                 * xpath路径为:/bookstore/book[@category='cooking']/title/@lang
                 */
                String str3=(String) xPath.evaluate("/bookstore/book[@category='cooking']/title/@lang", document, XPathConstants.STRING);
                System.out.println(str3);
                /**
                 *获取 bookstore 节点下所有 book 的节点集合
                 */
                NodeList nodeBookList=(NodeList) xPath.evaluate("/bookstore/book", document, XPathConstants.NODESET);
                for(int i=0;i<nodeBookList.getLength();i++){
                    Element element=(Element) nodeBookList.item(i);
                    String titleValue=(String) xPath.evaluate("title", element, XPathConstants.STRING);
                    String authorValue=(String) xPath.evaluate("author", element, XPathConstants.STRING);
                    String yearValue=(String) xPath.evaluate("year", element, XPathConstants.STRING);
                    String priceValue=(String) xPath.evaluate("price", element, XPathConstants.STRING);
                    System.out.println(titleValue+" "+authorValue+" "+yearValue+" "+priceValue);
                    System.out.println("=========================================");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    结果截图:

     

    如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

  • 相关阅读:
    Django -- 10.Django和Ajax
    Django -- 9.模型层(2)
    Django -- 8.模型层(1)
    Django -- 7.模板层
    Django -- 6.视图层
    Django -- 5.路由层(URLconf)_基于Django2
    Django -- 4.Django简介
    Django -- 3.web框架
    Delphi中Chrome Chromium、Cef3学习笔记(四)
    Delphi中Chrome Chromium、Cef3学习笔记(三)
  • 原文地址:https://www.cnblogs.com/aeon/p/10770718.html
Copyright © 2011-2022 走看看