Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML comments with DOM

I need to parse XML Tags which are commented out like

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

But all I found was setIgnoringComments(boolean)

Document doc = docBuilder.parse(new File(PathChecker.getDataTypesFile()));
docFactory.setIgnoringComments(true); // ture or false, no difference

But it doesn't seem to change anything. Is there any other way to parse this comments? I have to use DOM.

Regards

like image 624
Michael Brenndoerfer Avatar asked Aug 16 '26 10:08

Michael Brenndoerfer


1 Answers

Method "setIgnoringComments" removed comments from DOM tree during parsing. With "setIgnoringComments(false)" you can get comment text like:

    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());
        }
    }
like image 124
pasha701 Avatar answered Aug 17 '26 23:08

pasha701