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());
            }
        }
    }
    
    
  • 相关阅读:
    ExtJs005继承
    ExtJs004define定义类
    ExtJS笔记
    解决vscode-pandoc插件生成pdf中文显示问题
    UDP学习笔记(.NET)
    WPF类库不能添加资源词典(xaml)的解决办法
    解决win10下获取操作系统版本为6.2.900(win8)的问题
    [转] Unit Test 访问Internal类型和方法
    VS2017使用小技巧(持续更新。。。)
    [转]Github遇到Permanently added the RSA host key for IP address '192.30.252.128' to the list of known host
  • 原文地址:https://www.cnblogs.com/fly-book/p/12972561.html
Copyright © 2011-2022 走看看