Search code examples
javaxml-parsingjdomxml-commentsjdom-2

JDom How to comment an existing line


I am currently struggling to comment an existing line with JDom, best I would like to comment an entire node.

    SAXBuilder jdomBuild = new SAXBuilder();
    jdomDoc = jdomBuild.build(fileLocation);
    Element root = jdomDoc.getRootElement();
    Element header = root.getChild("header_info")
    // how can I comment the lines now?

And the document :

<xml_report>

    <header_info>
        <bla1></bla1>
        <bla2></bla2>
        <bla3></bla3>
        <bla4></bla4>
        <bla5></bla5>
        <bla6></bla6>
    </header_info>

    <random_things>
        <random></random>
    </random_things>

</xml_report>

I would like to comment the whole header but I can't find the solution anywhere...

Could I have some advice and explainations please?


Solution

  • Replace the header element with a Comment containing the content of the element. Example:

    XMLOutputter outputter = new XMLOutputter();
    outputter.getFormat().setExpandEmptyElements(true);
    SAXBuilder jdomBuild = new SAXBuilder();
    Document jdomDoc = jdomBuild.build(new ByteArrayInputStream(("<xml_report>\n"
            + "\n"
            + "    <header_info>\n"
            + "        <bla1></bla1>\n"
            + "        <bla2></bla2>\n"
            + "        <bla3></bla3>\n"
            + "        <bla4></bla4>\n"
            + "        <bla5></bla5>\n"
            + "        <bla6></bla6>\n"
            + "    </header_info>\n"
            + "\n"
            + "    <random_things>\n"
            + "        <random/>\n"
            + "    </random_things>\n"
            + "\n"
            + "</xml_report>").getBytes(StandardCharsets.UTF_8)));
    Element root = jdomDoc.getRootElement();
    Element header = root.getChild("header_info");
    Comment comment = new Comment(outputter.outputString(header));
    root.setContent(root.indexOf(header), comment);
    outputter.output(jdomDoc, System.out);
    

    Output:

    <?xml version="1.0" encoding="UTF-8"?>
    <xml_report>
    
        <!--<header_info>
            <bla1></bla1>
            <bla2></bla2>
            <bla3></bla3>
            <bla4></bla4>
            <bla5></bla5>
            <bla6></bla6>
        </header_info>-->
    
        <random_things>
            <random></random>
        </random_things>
    
    </xml_report>
    

    Note that you cannot preserve the fromatting with JDOM for every possible input, since the formatting information is removed during the parsing.

    Also note that you cannot put a comment around a block containing a comment, since this would end the comment sooner. In fact JDOM does not allow -- to occur as substring in a comment. You could simply break up those substings inside comments using

    private static final Pattern PATTERN = Pattern.compile("-{2,}"); 
    
    private static String fix(String string) {
        StringBuilder sb = new StringBuilder();
        Matcher m = PATTERN.matcher(string);
        int lastEnd = 0;
        while (m.find()) {
            System.out.println(m.group());
            sb.append(string.subSequence(lastEnd, m.start())).append('-');
            lastEnd = m.end();
            for (int i = lastEnd - m.start(); i > 1; i--) {
                sb.append(" -");
            }
        }
        if (lastEnd < string.length()) {
            sb.append(string.subSequence(lastEnd, string.length()));
        }
        return sb.toString();
    }
    
    Comment comment = new Comment(fix(outputter.outputString(header)));
    

    Anything else would get compicated, since you'd need to take <![CDATA[]]> into account too.