0

reading the answer here : Normalization in DOM parsing with java - how does it work?

I understand that the normalization will remove empty adjacent text nodes, I tried the following xml :

<company>hello
wor
ld
</company>

with the following code :

    try {
        DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();


        Document doc = dBuilder.parse(file);
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        System.out.println(doc.getDocumentElement().getChildNodes().getLength());
        System.out.println(doc.getDocumentElement().getChildNodes().item(0).getTextContent());

    } catch (Exception e) {
        e.printStackTrace();
    }

I always get 1 child node for the element "company" even without normalize. the result is :

Root element :company
1
hello
wor
ld

so what is wrong here ? can anyone explain ? shouldn't I get hello world in one line.

Mohammad Karmi
  • 1,403
  • 3
  • 26
  • 42

1 Answers1

1

The parser is already creating a normalized DOM tree.

The normalize() method is useful for when you're building/modifying the DOM, which might not result in a normalized tree, in which case the method will normalize it for you.

Common Helper

private static void printDom(String indent, Node node) {
    System.out.println(indent + node);
    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling())
        printDom(indent + "  ", child);
}

Example 1

public static void main(String[] args) throws Exception {
    String xml = "<Root>text 1<!-- test -->text 2</Root>";
    DocumentBuilder domBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = domBuilder.parse(new InputSource(new StringReader(xml)));
    printDom("", doc);
    deleteComments(doc);
    printDom("", doc);
    doc.normalizeDocument();
    printDom("", doc);
}
private static void deleteComments(Node node) {
    if (node.getNodeType() == Node.COMMENT_NODE)
        node.getParentNode().removeChild(node);
    else {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++)
            deleteComments(children.item(i));
    }
}

Output

[#document: null]
  [Root: null]
    [#text: text 1]
    [#comment:  test ]
    [#text: text 2]
[#document: null]
  [Root: null]
    [#text: text 1]
    [#text: text 2]
[#document: null]
  [Root: null]
    [#text: text 1text 2]

Example 2

public static void main(String[] args) throws Exception {
    DocumentBuilder domBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = domBuilder.newDocument();
    Element root = doc.createElement("Root");
    doc.appendChild(root);
    root.appendChild(doc.createTextNode("Hello"));
    root.appendChild(doc.createTextNode(" "));
    root.appendChild(doc.createTextNode("World"));
    printDom("", doc);
    doc.normalizeDocument();
    printDom("", doc);
}

Output

[#document: null]
  [Root: null]
    [#text: Hello]
    [#text:  ]
    [#text: World]
[#document: null]
  [Root: null]
    [#text: Hello World]
Andreas
  • 154,647
  • 11
  • 152
  • 247