当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > DOM Mozilla 和 IE
Both Mozilla and Internet Explorer supports W3C's DOM specification.
Mozilla 和 IE 都支持W3C's DOM 规范。
However, there are still differences between Internet Explorer's DOM and Mozilla's DOM. The most important difference is how they handle white-space text nodes. When XML generates, it often contains white-spaces between the nodes. Internet Explorer, when using node.childNodes[], will NOT contain these white-space nodes. In Mozilla, those nodes will be in the array.
然而,IE 和 Mozilla 对 DOM 的处理方式是不同的,最大的区别就是在空格文本节点处理上的差异。当生成XML时,通常会在节点之间包含空格。在IE中,我们可以使用“node.childNodes[]”去掉这些空格;在Mozilla中,这些节点将存在于数组中。
请看看下面的这个XML文件:books.xml
The following code fragment alerts how many child nodes the root element have:
下述代码片断将会提示根元素中包含的子节点数量:
xmlDoc.load("books.xml"); alert(x.length) for (i=0;i<x.length;i++) |
Internet Explorer will skip the white-space text nodes that are generated between nodes (e.g. new line characters), while Mozilla will not. So, in the example above, Mozilla browsers will alert 9 child nodes, while Internet Explorer will alert 4.
IE 将会跳过节点之间生成的空格文本节点(例如:新行符);然而,Mozilla 并不会这样做。在上述案例中,Mozilla 浏览器会提示9个子节点,但是IE只会提示4个。
To iterate through the child nodes and disregard those text nodes, you can check the node type. An element node has type 1, a text node has type 3, and a comment node has type 8. To skip text nodes, you can process only nodes that are not of node type 3 (text nodes) and node type 8 (comment nodes):
通过对子节点的重申并忽略这些文本节点,你可以查阅节点类型。元素节点为“类型1”;文本节点为“类型3”;注释节点为“类型8”。你可以仅通过处理非文本节点(类型3)和注释节点(类型8)的节点来跳过对文本节点的处理,具体如下:
xmlDoc.load("books.xml"); for (var i=0;i<x.length;i++) |
The best way to only process element nodes is to iterate through the child nodes and only process those with a node type of 1:
仅对元素节点进行处理的最好方法就是通过重申子节点并仅对“类型1”的节点进行处理,具体如下:
xmlDoc.load("books.xml"); for (var i=0;i<x.length;i++) |
如果你想学习完整的节点类型参数以及它们的子节点参数,可以查阅我们的节点类型参数。