gpt4 book ai didi

Java 将 NodeList 转换为 String 以检查 xml 注释

转载 作者:行者123 更新时间:2023-12-02 09:03:50 25 4
gpt4 key购买 nike

我需要将现有 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>

关于Java 将 NodeList 转换为 String 以检查 xml 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59975971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com