当前位置: 技术问答>java相关
DOM读取出来的 NodeList 的顺序是否一定和XML文件中保持一致?
来源: 互联网 发布时间:2017-04-26
本文导语: 假设有一XML文件(有关班车线路,所以顺序一定要正确) 片断如下 ....... stop1 stop2 stop3 stop4 stop5 ...... 如果我用DOM 的getChildNodes()取得 "stop" 的 NodeList,或者用 getFirstChild(),getNextSibling()取得的Node , 是否能保证顺序一定和XML...
假设有一XML文件(有关班车线路,所以顺序一定要正确)
片断如下
.......
stop1
stop2
stop3
stop4
stop5
......
如果我用DOM 的getChildNodes()取得 "stop" 的 NodeList,或者用 getFirstChild(),getNextSibling()取得的Node , 是否能保证顺序一定和XML文件中的一样?
最好能给出相关文档的连接。谢谢!
|
getChildNodes()返回NodeList,NodeList 是有序的,但在不同的实现中其顺序也许不同。请参考下面两段
Interface NodeList
The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. NodeList objects in the DOM are live
The DOM also specifies a NodeList interface to handle ordered lists of Nodes, such as the children of a Node, or the elements returned by the getElementsByTagName method of the Element interface, and also a NamedNodeMap interface to handle unordered sets of nodes referenced by their name attribute, such as the attributes of an Element. NodeList and NamedNodeMap objects in the DOM are live; that is, changes to the underlying document structure are reflected in all relevant NodeList and NamedNodeMap objects. For example, if a DOM user gets a NodeList object containing the children of an Element, then subsequently adds more children to that element (or removes children, or modifies them), those changes are automatically reflected in the NodeList, without further action on the user's part. Likewise, changes to a Node in the tree are reflected in all references to that Node in NodeList and NamedNodeMap objects.
参见
http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html
Interface NodeList
The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. NodeList objects in the DOM are live
The DOM also specifies a NodeList interface to handle ordered lists of Nodes, such as the children of a Node, or the elements returned by the getElementsByTagName method of the Element interface, and also a NamedNodeMap interface to handle unordered sets of nodes referenced by their name attribute, such as the attributes of an Element. NodeList and NamedNodeMap objects in the DOM are live; that is, changes to the underlying document structure are reflected in all relevant NodeList and NamedNodeMap objects. For example, if a DOM user gets a NodeList object containing the children of an Element, then subsequently adds more children to that element (or removes children, or modifies them), those changes are automatically reflected in the NodeList, without further action on the user's part. Likewise, changes to a Node in the tree are reflected in all references to that Node in NodeList and NamedNodeMap objects.
参见
http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html