【问题标题】:Normalization DOM same effect without normalize规范化 DOM 相同的效果没有规范化
【发布时间】:2018-12-26 16:59:22
【问题描述】:

在这里阅读答案: Normalization in DOM parsing with java - how does it work?

我知道规范化会删除空的相邻文本节点,我尝试了以下 xml:

<company>hello
wor
ld
</company>

使用以下代码:

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

即使没有规范化,我也总是为元素“公司”获得 1 个子节点。结果是:

Root element :company
1
hello
wor
ld

那么这里有什么问题吗?谁能解释一下?我不应该在一行中打招呼吗?

【问题讨论】:

    标签: java xml dom


    【解决方案1】:

    解析器已经在创建一个规范化的 DOM 树。

    normalize() 方法在您构建/修改 DOM 时很有用,这可能不会生成规范化的树,在这种情况下,该方法会为您规范化它。

    常用助手

    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);
    }
    

    示例 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));
        }
    }
    

    输出

    [#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]
    

    示例 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);
    }
    

    输出

    [#document: null]
      [Root: null]
        [#text: Hello]
        [#text:  ]
        [#text: World]
    
    [#document: null]
      [Root: null]
        [#text: Hello World]
    

    【讨论】:

      猜你喜欢
      • 2012-05-22
      • 1970-01-01
      • 2017-03-05
      • 2013-08-21
      • 2016-05-13
      • 2018-06-06
      • 2010-12-29
      • 2022-09-28
      相关资源
      最近更新 更多