I’m having a problem removing an XML element from my doc. I have searched the forums and I think I’m doing things properly, please can you help shed some light on where I’m screwing up?
I have an array containing IDs which I’m looping through. Within that loop I’m looping through the XML doc to find elements whose “myId” attribute has the same ID as in the array.
When I find one I want to remove that element from the XML doc.
Here’s my code:
var xmlFilename=document.getElementById('xmlFilename').value;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",xmlFilename,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
// lets get all the xml elements into xAll
var xAll=xmlDoc.getElementsByTagName('*');
// lets use the buttonList array - this corresponds to the elements in the xml to hide
for (var i=0; i<buttonList.length;i++) {
alert ("Looking for "+buttonList[i]);
//find the XML node with the same id
for(var j=0;j<xAll.length;j++) {
y=xAll[j];
if (y.getAttribute('myId')==buttonList[i]) {
alert('Found a match');
xmlDoc.documentElement.removeChild(y);
alert('removed');
}
}
alert('next!');
}
alert('all done');
It loops through as expected, but when it finds a match the xmlDoc.documentElement.removeChild(y);
line causes the script to fail and it never gets to ‘removed’ state.
I am grateful for any help/pointers in the right direction.
Thanks, Mark
try: