java - 使用 java 进行 DOM 解析的规范化 - 它是如何工作的?

标签 java xml dom

我在 this tutorial 的 DOM 解析器的代码中看到了以下行.

doc.getDocumentElement().normalize();

我们为什么要进行这种标准化?
我读了docs但我一个字都听不懂。

Puts all Text nodes in the full depth of the sub-tree underneath this Node

好的,那么谁能给我看看(最好有图片)这棵树长什么样子?

谁能解释一下为什么需要标准化?
如果我们不规范化会发生什么?

最佳答案

句子的其余部分是:

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.

这基本上意味着下面的 XML 元素

<foo>hello 
wor
ld</foo>

在非规范化节点中可以这样表示:

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

当归一化时,节点将如下所示

Element foo
    Text node: "Hello world"

属性也是如此:<foo bar="Hello world"/> 、评论等。

关于java - 使用 java 进行 DOM 解析的规范化 - 它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13786607/

相关文章:

java - 将无效的xml元素添加到xml文档java

java - 格式化数字字符串

java - AWT 绘画工具像素不完美

java - 注释如何访问参数信息?

java - 按住按钮进行重复操作——是否有 android :onClick for holds?(在 XML 文件中)

c# - 由于格式化,XML 反序列化在十进制解析时崩溃

java - 当两个客户端向服务器发送对象时,ObjectOutputStream writeObject 挂起

JavaScript - 浏览器立即回流和重绘

jquery - 如何以编程方式设置 jQuery 下拉检查列表的值?

javascript - 无法更改 <template> 中第一个元素的 div ID