1 Reply Latest reply on May 14, 2009 11:08 AM by _Natasha_

    Looping and Deleting XML Nodes

    midimid Level 1

      A bit perplexed about what is likely a simple problem.

       

      Say I have the following XML (myXML):

      <stories>

      <story id="1"/>

      <story id="2"/>

      <story id="3"/>

      <otherNode id="15"/>

      <otherNode id="15"/>

      </stories>

       

      I want to delete nodes with id's of one and two.

       

      So I currently have:

      for (var nodeIndex in myXML.story) {

      var nodeValue = myXML.story[nodeIndex];

      if (nodeValue != undefined) {

      if (nodeValue.@id == "1" || nodeValue.@id == "2") {

      delete myXML.story[nodeIndex];

      }

      }

      }

       

       

      The problem: After deleting the node with id="1" the very next iteration is the node with id="3" - the node with id="2" is never read.

       

      While it totally makes sense to me, my only dummified solution was to switch to a numeric loop and use nodeIndex-- when I delete a node. But that seems rather unelegant.