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());
            }
        }
    }
    
    
  • 相关阅读:
    收藏的一个Sqlserver性能查询,包括查询CPU 网络等
    转载自博客园的一篇文章 通过SQL Server Profiler来监视分析死锁
    关于Sqlserver的换行和空格
    Sql Server查询性能优化之不可小觑的书签查找
    临时表和表变量,转载自博客园
    Sqlserver活动视图
    代码列表 4.5:显示累计最消耗 CPU 时间的前50个运行计划
    关于Sqlserver2012分页的新功能尝试
    sqlserver 东八时区的英文时间转换
    Flash应用效率优化启示录Ⅰ
  • 原文地址:https://www.cnblogs.com/fly-book/p/12972561.html
Copyright © 2011-2022 走看看