zoukankan      html  css  js  c++  java
  • XPath实例

    <?xml version="1.0" encoding="UTF-8"?>
    <inventory>
        <book year="2000">
            <title>Snow Crash</title>
            <author>Neal Stephenson</author>
            <publisher>Spectra</publisher>
            <isbn>0553380958</isbn>
            <price>14.95</price>
        </book>
        <book year="2005">
            <title>Burning Tower</title>
            <author>Larry Niven</author>
            <author>Jerry Pournelle</author>
            <publisher>Pocket</publisher>
            <isbn>0743416910</isbn>
            <price>5.99</price>
        </book>
        <book year="1995">
            <title>Zodiac</title>
            <author>Neal Stephenson</author>
            <publisher>Spectra</publisher>
            <isbn>0553573862</isbn>
            <price>7.50</price>
        </book>
    </inventory>
    
    
    
    public class XPathTest {
        @Test
        public void test() throws Exception {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            builderFactory.setValidating(true);
            builderFactory.setNamespaceAware(false);
            builderFactory.setIgnoringComments(true);
            builderFactory.setIgnoringElementContentWhitespace(false);
            builderFactory.setCoalescing(false);
            builderFactory.setExpandEntityReferences(true);
    
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            builder.setErrorHandler(new ErrorHandler() {
                @Override
                public void warning(SAXParseException exception) throws SAXException {
                    System.out.println("warning+"+exception.getMessage());
                }
    
                @Override
                public void error(SAXParseException exception) throws SAXException {
                    System.out.println("error+"+exception.getMessage());
                }
    
                @Override
                public void fatalError(SAXParseException exception) throws SAXException {
                    System.out.println("fatalError+"+exception.getMessage());
                }
            });
    
            Document document = builder.parse("cn.test.inventory.xml");
            XPathFactory factory = XPathFactory.newInstance();
            XPath xPath = factory.newXPath();
            XPathExpression expression = xPath.compile("//book[author='Neal Stephenson']/title/text()");
            Object res = expression.evaluate(document, XPathConstants.NODESET);
            System.out.println("查询作者为 Neal Stephenson 的图书的标题: ");
            NodeList nodes = (NodeList) res;
            for (int i = 0; i < nodes.getLength(); i++) {
                System.out.println(nodes.item(i).getNodeValue());
            }
    
            System.out.println("查询 1997 年之后的图书的标题:");
            nodes = (NodeList) xPath.evaluate("//book[@year>1997]/title/text()",document,XPathConstants.NODESET);
            for (int i = 0; i < nodes.getLength(); i++) {
                System.out.println(nodes.item(i).getNodeValue());
            }
    
            System.out.println("查询 1997 年之后的图书的属性和标题:");
            nodes = (NodeList) xPath.evaluate("//book[@year>1997]/@*|//book[@year>1997]/title/text()",document,XPathConstants.NODESET);
            for (int i = 0; i < nodes.getLength(); i++) {
                System.out.println(nodes.item(i).getNodeValue());
            }
        }
    }
    
    
  • 相关阅读:
    笔记本无线网卡和有线网卡同时用及网络知识回顾总结
    DSPack初次使用小结
    常见加解密算法及Delphi应用程序图标总结
    Delphi窗体创建释放过程及单元文件小结
    怪异的JavaScript的Case语句
    交换机与路由器的区别
    DirectShow学习笔记总结
    Git的提交与查看差异
    Laravel 5使用Laravel Excel实现Excel/CSV文件导入导出的功能详解
    laravel5的Bcrypt加密方式对系统保存密码的小结
  • 原文地址:https://www.cnblogs.com/fly-book/p/12972561.html
Copyright © 2011-2022 走看看