我试图读取XML文件中的注释。在使用DOM解析后无法获取注释节点。
代码:
DocumentBuilderFactory docBldrFactry = DocumentBuilderFactory.newInstance();
docBldrFactry.setIgnoringComments(false);
DocumentBuilder docBuilder = docBldrFactry.newDocumentBuilder();
Document document = docBuilder.parse(new File("C:\\webser.xml"));
Element rootElement = document.getDocumentElement();
NodeList list = rootElement.getElementsByTagName("Bean");
评论在Bean元素下。当我遍历Bean元素的子节点时,我没有得到注释节点。
这是XML
<Beans>
<Bean>
<!-- Testing Comments -->
<API name ="xyz" />
</Bean>
</Beans>
答案 0 :(得分:5)
希望这有帮助:)
XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream("file.xml"));
while (xr.hasNext()) {
if (xr.next() == XMLStreamConstants.COMMENT) {
String comment = xr.getText();
} }