Java 将 NodeList 转换为字符串以检查 xml 评论

Java Convert the NodeList to String to check for xml comments

我需要将我的现有 NodeList 转换为字符串,以便我可以检查它是否在断言中包含 xml 注释。

这是我的 xml:

<document>
<org1>
<!---- I am a comment ----->
<somenNode1> hello </somenode1>
<somenNode2> hello </somenode1>
</org1>

</document>

我在 NodeList 中,需要转换成字符串,这样我才能检查它是否包含注释。

这是我的代码

public static NodeList allNodes (final Node document, final String xPath) {
        final XPathFactory factory = XPathFactory.newInstance();
        final XPath xpath = factory.newXPath();
        final NodeList result;
        try {
            final XPathExpression productXpath = xpath.compile(xPath);
            result = (NodeList)productXpath.evaluate(document, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {

            throw new RuntimeException(e);
        }
        return result;
    }



 private static void verifyXpathContains (final String expected, final String xPath, org.w3c.dom.Document xmlDoc) {
NodeList nodeList = XPathUtils.allNodes(xmlDoc, xPath);

assertThat(nodeList.toString()).contains(expected);

}

遍历NodeList,得到每个子类型,然后递归入树,比较简单

public static List<Node> extractComments(final NodeList search) {
    List<Node> result = new ArrayList<>();
    for (int i = 0, length = search.getLength(); i < length; i++) {
        Node child = search.item(i);
        if (child.getNodeType() == Node.COMMENT_NODE) {
            result.add(child);
        }
        result.addAll(extractComments(child.getChildNodes()));
    }
    return result;
}

或者如果您只对原始字符串感兴趣...

public static List<String> extractComments(final NodeList search) {
    List<String> result = new ArrayList<>();
    for (int i = 0, length = search.getLength(); i < length; i++) {
        Node child = search.item(i);
        if (child.getNodeType() == Node.COMMENT_NODE) {
            result.add(child.getTextContent());
        }
        result.addAll(extractComments(child.getChildNodes()));
    }
    return result;
}

对于您上面和这个的输入

NodeList result = XPathUtils.allNodes(document, "*/org1");
System.out.println(extractComments(result));

结果是

[[#comment:  I am a comment ]]

我还注意到您输入的源代码中存在一些语法错误

<document>
<org1>
<!---- I am a comment ----->      <== additional "--" is illegal inside a comment
<somenNode1> hello </somenode1>   <== close tag should match open tag
<somenNode2> hello </somenode1>   <== close tag should be somenNode2
</org1>

</document>