parse xml using dom java

前端 未结 3 1975
青春惊慌失措
青春惊慌失措 2021-01-25 20:15

I have the bellow xml:


    
        
            wish
             


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-25 20:57

    First, when checking the node name you shouldn't compare Strings using ==. Always use the equals method instead.

    You can use XPath to evaluate only the document topic elements under listOfDocs:

    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();
    XPathExpression xPathExpression = xPath.compile("//listOfDocs//document/topic");
    
    NodeList topicnl = (NodeList) xPathExpression.evaluate(dom, XPathConstants.NODESET);
    for(int i = 0; i < topicnl.getLength(); i++) {
       ...
    

提交回复
热议问题