Normalization in DOM parsing with java - how does it work?

前端 未结 3 1879
一向
一向 2020-11-22 04:48

I saw the line below in code for a DOM parser at this tutorial.

doc.getDocumentElement().normalize();

Why do we do this normalization ?

3条回答
  •  悲哀的现实
    2020-11-22 05:16

    The rest of the sentence is:

    where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes.

    This basically means that the following XML element

    hello 
    wor
    ld
    

    could be represented like this in a denormalized node:

    Element foo
        Text node: ""
        Text node: "Hello "
        Text node: "wor"
        Text node: "ld"
    

    When normalized, the node will look like this

    Element foo
        Text node: "Hello world"
    

    And the same goes for attributes: , comments, etc.

提交回复
热议问题