Removing DOM nodes when traversing a NodeList

前端 未结 7 904
南方客
南方客 2020-12-06 04:54

I\'m about to delete certain elements in an XML document, using code like the following:

NodeList nodes = ...;
for (int i = 0; i < nodes.getLength(); i++)         


        
7条回答
  •  渐次进展
    2020-12-06 05:30

    According to the DOM specificaion, the result of a call to node.getElementsByTagName("...") is supposed to be "live", that is, any modification made to the DOM tree will be reflected in the NodeList object. Well, for conforming implementations, that is...

    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.

    (DOM Specification)

    So, when you modify the tree structure, a conforming implementation will change the NodeList to reflect these changes.

提交回复
热议问题