XPath - how to get a list of multiple tags with the same name

后端 未结 2 1877
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 13:33


    
        Nike
        13
        White

        
相关标签:
2条回答
  • 2021-01-29 14:19

    With //shoes/additional you would get a list of all the nodes and could iterate over it in Java.

    0 讨论(0)
  • 2021-01-29 14:22

    Here's an example that uses XPath from the JDK to execute a query. It assumes that the variable xml contains your XML document.

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate("//shoes/additional", new InputSource(new StringReader(xml)), XPathConstants.NODESET);
    for (int i = 0; i < nodeList.getLength(); ++i) {
        String name = nodeList.item(i).getAttributes().getNamedItem("name").getNodeValue();
        String text = nodeList.item(i).getTextContent();
        System.out.println(name + ", " + text);
    }
    
    0 讨论(0)
提交回复
热议问题