Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

My xml file is like:

it contains different 'object' nodes and in different objects there are different parameters one is deleted parameter.

I want to delete the all 'object' nodes that contains the deleted parameter 1.

This is the code that deletes the node object which has a parameter node deleted =1:

x=xmlDoc.documentElement;
for(var count=0; count<5;count++){
  var y=x.getElementsByTagName("deleted")[count]; //Find that nodes arent
  if(y.textContent == "1") {
    var z=y.parentNode; //delete the node from the parent.
    x.removeChild(z);
    Xml2String1= new XMLSerializer().serializeToString(x);
  }
}
share|improve this question
1  
And what have you tried? where did you stuck? – gdoron Jul 12 '13 at 14:27
    
your xml, js codeblock isn't showing... – sonjz Jul 12 '13 at 14:30
    
thanks for the consideration, I again tried and my method works this is the code that deletes the node object which has a parameter node deleted =1: " x=xmlDoc.documentElement; for(var x1=0; x1<5;x1++){ var y=x.getElementsByTagName("deleted")[x1]; //Find that nodes parent if(y.textContent == "1") { var z=y.parentNode; //delete the node from the parent. x.removeChild(z); Xml2String1= new XMLSerializer().serializeToString(x); " thanks – Zeeshan Jul 12 '13 at 14:46
2  
Please change x1 to i or index or something sane – raam86 Jul 12 '13 at 15:11
    
no i think it works since x1 is the loop variable and it checks for the all nodes that does they carry any deleted 1 so if they have they delete their parent node form the xml – Zeeshan Jul 12 '13 at 15:13
up vote 0 down vote accepted

Your loop is incorrect:

for(var x1=0; x1<5;x1++){
  var y=x.getElementsByTagName("deleted")[x1];

Your loop runs for 5 iterations without regard for the number of <deleted> elements are found. Each time through the loop you search again and get a new NodeList/HTMLCollection of the remaining <deleted> elements, but your loop counter is incremented regardless.

Try this instead:

var deletedNodesList = x.getElementsByTagName("deleted");
var nodesToDelete = [];
for (var index = 0; index < deletedNodes.length ; index += 1)
    {
    var node = deletedNodes[index];
    if (node.textContent == "1")
        {
        nodesToDelete.push( node.parentNode ); //delete the node from the parent
        }
    }

nodesToDelete.forEach( function() { x.removeChild(this); } );

Note that, per the documentation on MDN, the NodeList is a live collection, so don't modify it while you are processing it.


PS. I second raam86's recommendation to use sane (meaningful) variable names. Meaningful variable names make it easier to understand the code, which makes it easier to write correct code and to resolve problems in incorrect code.

share|improve this answer
    
the loop was set to 5 for the testing purpose. – Zeeshan Jul 15 '13 at 12:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.