我需要解析被注释掉的XML标签
但我发现的只有setIgnoringComments(boolean)
Document doc = docBuilder.parse(new File(PathChecker.getDataTypesFile()));
docFactory.setIgnoringComments(true); // ture or false, no difference
但它似乎没有改变任何东西.
有没有其他方法来解析这些评论?我必须使用DOM.
问候
解决方法:
方法“setIgnoringComments”在解析期间从DOM树中删除了注释.
使用“setIgnoringComments(false)”,您可以获得以下注释文本:
NodeList nl = doc.getDocumentElement().getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
if (nl.item(i).getNodeType() == Element.COMMENT_NODE) {
Comment comment=(Comment) nl.item(i);
System.out.println(comment.getData());
}
}
标签:java,dom,xml,parsing,xml-parsing
来源: https://codeday.me/bug/20190831/1774961.html