Java Node normalize 方法有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2457955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does Java Node normalize method do?
提问by Tom Brito
I'm doing some tests, but I see no difference when I use or not the normalize()
method.
But the examples at ExampleDepot website use it.
So, what is it for? (The documentation wasn't clear for me either)
我正在做一些测试,但是当我使用或不使用该normalize()
方法时,我看不出有什么区别。但是 ExampleDepot 网站上的示例使用了它。那么,它有什么用呢?(文档对我来说也不清楚)
采纳答案by Michael Borgwardt
You can programmatically build a DOM tree that has extraneous structure not corresponding to actual XML structures - specifically things like multiple nodes of type text next to each other, or empty nodes of type text. The normalize()
method removes these, i.e. it combines adjacent text nodes and removes empty ones.
您可以以编程方式构建具有与实际 XML 结构不对应的无关结构的 DOM 树 - 特别是诸如文本类型的多个节点彼此相邻或文本类型的空节点之类的东西。该normalize()
方法移除这些,即它组合相邻的文本节点并移除空的节点。
This can be useful when you have other code that expects DOM trees to always look like something built from an actual XML document.
当您有其他代码希望 DOM 树看起来总是像从实际 XML 文档构建的东西时,这会很有用。
This basically means that the following XML element
这基本上意味着以下 XML 元素
<foo>hello
wor
ld</foo>
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"
回答by Artic
It cleans code from adjacent Text nodes and empty Text nodes
它从相邻的 Text 节点和空的 Text 节点中清除代码
回答by Dhiral Pandya
Normalize the root element of the XML document. This ensures that all Text nodes under the root node are put into a "normal" form, which means that there are neither adjacent Text nodes nor empty Text nodes in the document.
规范化 XML 文档的根元素。这样可以确保根节点下的所有 Text 节点都放入“正常”形式,这意味着文档中既没有相邻的 Text 节点,也没有空的 Text 节点。
回答by Fabian
there are a lot of possible DOM trees that correspond to the same XML structure and each XML structure has at least one corresponding DOM tree. So conversion from DOM to XML is surjective. So it may happen that:
有很多可能的 DOM 树对应相同的 XML 结构,每个 XML 结构至少有一个对应的 DOM 树。所以从 DOM 到 XML 的转换是满射的。所以可能会发生:
dom_tree_1 != dom_tree_2
# but:
dom_tree_1.save_DOM_as_XML() == dom_tree_2.save_DOM_as_XML()
And there is no way for ensuring:
并且无法确保:
dom_tree == dom_tree.save_DOM_as_XML().load_DOM_from_XML()
But we would like to have it bijective. That means each XML structure corresponds to one particular DOM tree.
但我们希望它是双射的。这意味着每个 XML 结构对应一个特定的 DOM 树。
So you can define a subset of all possible DOM trees that is bijective to the set of all possible XML structures.
因此,您可以定义所有可能的 DOM 树的子集,该子集与所有可能的 XML 结构的集合成双射。
# still:
dom_tree.save_DOM_as_XML() == dom_tree.normalized().save_DOM_as_XML()
# but with:
dom_tree_n = dom_tree.normalize()
# we now even have:
dom_tree_n == dom_tree_n.save_DOM_as_XML().load_DOM_from_XML().normalize()
So normalized DOM trees can be perfectly reconstructed from their XML representation. There is no information loss.
因此,规范化的 DOM 树可以从它们的 XML 表示中完美地重建。没有信息丢失。