使用DOM解析XML注释

3

我需要解析被注释掉的XML标签,例如

<DataType Name="SecureCode" Size="4" Type="NVARCHAR">
    <!-- <Validation>
            <Regex JavaPattern="^[0-9]*$" JSPattern="^[0-9]*$"/>
    </Validation> -->
    <UIType Size="4" UITableSize="4"/>
</DataType>

但我所找到的只有setIgnoringComments(boolean)
Document doc = docBuilder.parse(new File(PathChecker.getDataTypesFile()));
docFactory.setIgnoringComments(true); // ture or false, no difference

但这似乎没有改变任何东西。 有没有其他方法来解析这些注释?我必须使用DOM。

问候


可能是重复的问题:http://stackoverflow.com/questions/12069509/how-to-read-comments-from-xml-file-using-dom-parser - Ankur Shanbhag
1
我也找到了那篇文章,但它对我没有任何帮助。 - Michael Brenndoerfer
看起来 setIgnoringComments() 存在一个 bug,已经有人报告了,但是这些年来还没有修复:https://issues.apache.org/jira/browse/XERCESJ-37 - Miguel Pardal
setIgnoringComments() 对我来说刚刚好用。 - john k
2个回答

5

方法“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());
        }
    }

哦,那很酷。我没试过,但应该可以解决问题。谢谢。 - Michael Brenndoerfer
这将不会选择单行注释。 - SharadxDutta

0

由于似乎不存在解决问题的“常规方法”,我刚刚删除了注释。

BufferedReader br = new BufferedReader(new FileReader(new File(PathChecker.getDataTypesFile())));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(PathChecker.getDataTypesFileWithoutComments())));

String line = "";

while ((line = br.readLine()) != null) {
    line = line.replace("<!--", "").replace("-->", "") + "\n";
    bw.write(line);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接