Xpath 解析xml

xpath语法参考:XPath 语法 | 菜鸟教程 

 

 

 

    @Test
    public void testXml() throws XPathExpressionException, ParserConfigurationException, IOException, SAXException {
        String xml = "<users>\n" +
                "    <user name=\"zs\" age=\"18\">\n" +
                "        <height>180</height>\n" +
                "        <weight>140</weight>\n" +
                "        <hobby>basketball1</hobby>\n" +
                "    </user>\n" +
                "    <user name=\"ls\" age=\"17\">\n" +
                "        <height>160</height>\n" +
                "        <weight>160</weight>\n" +
                "        <hobby>basketball2</hobby>\n" +
                "    </user>\n" +
                "</users>";


        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression expr = xpath.compile("//user/hobby[contains(text(),'basketball3')]");
        boolean flag = (Boolean)expr.evaluate(doc, XPathConstants.BOOLEAN);
        Assert.assertEquals(flag,true);
        System.out.println(flag);
    }

    @Test
    public void testXml1() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {

        String xml = "<users>\n" +
                "    <user name=\"zs\" age=\"18\">\n" +
                "        <height>180</height>\n" +
                "        <weight>140</weight>\n" +
                "        <hobby>basketball</hobby>\n" +
                "    </user>\n" +
                "    <user name=\"ls\" age=\"17\">\n" +
                "        <height>160</height>\n" +
                "        <weight>160</weight>\n" +
                "        <hobby>basketball</hobby>\n" +
                "    </user>\n" +
                "</users>";

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
        System.out.println(doc.getChildNodes().getLength());
        XPathFactory xFactory = XPathFactory.newInstance();
        XPath xpath = xFactory.newXPath();
        XPathExpression expr = xpath
                .compile("//user[@name='zs']/height");
        String height = (String)expr.evaluate(doc, XPathConstants.STRING);
        System.out.println(height);
    }

    @Test
    public void testXml2() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {

        String xml = "<users>\n" +
                "    <user name=\"zs\" age=\"18\">\n" +
                "        <height>180</height>\n" +
                "        <weight>140</weight>\n" +
                "        <hobby>basketball</hobby>\n" +
                "    </user>\n" +
                "    <user name=\"ls\" age=\"17\">\n" +
                "        <height>160</height>\n" +
                "        <weight>160</weight>\n" +
                "        <hobby>basketball</hobby>\n" +
                "    </user>\n" +
                "</users>";

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
        System.out.println(doc.getChildNodes().getLength());
        XPathFactory xFactory = XPathFactory.newInstance();
        XPath xpath = xFactory.newXPath();
        XPathExpression expr = xpath
                .compile("//user[@name='zs']");

        NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        System.out.println(nodes.getLength() );
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            System.out.println(node.getNodeValue()==null? node.getTextContent():node.getNodeValue());
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值