Oh my god. It's full of code!

Simple XML Node Delete in Javascript

Hey everyone, I found the answer to another problem that had been bugging me for a while. When you want to delete a node from an XML file using javascript. Many examples only talk about how to remove an element when it is directly under the root node. Well that doesn’t work if your XML is at all deep. For those who just want the code it goes like this

//Find the node to kill
var y=XML.getElementsByTagName("NodeToDelete")[0];
//Find that nodes parent
var z=y.parentNode;
//delete the node from the parent.
z.removeChild(y);

Just replace NodeToDelete with whatever XML object you are trying to get rid of. A few things to know

– You only need to get the parent directly above the node you are trying to get rid of, you do not need to get the entire hierarchy.

– You can delete single attributes, or entire nodes using this.

– Remember if you delete nodes, your counters and stuff might get thrown off. If you delete node 3, what was 4 is now 4, what was 5 is now 4, etc. To help combat this issue, I wrote this quick little function

function FindNodeIndex(ID)
{
    //This function finds the index position of a node based on a given ID number. Normally an ID will be the same as the node index,
    //IE object0 will have an ID of 0 for the object, and it will also be first (0th) node in the XML. However, if objects are deleted
    //then the ID of the object and it's position in the XML may no longer match. If element 4 was deleted, element 5 still has an ID of
    //5, but it is now the 4th element in the XML. So with this function you can find the actual index of a node by passing in the ID.
    
    //set a default return number in case no node is found with the given id
    var nodePos = -1;
    
    //find how many nodes there are so we can loop over them.
    var XMLLength = XML.getElementsByTagName("id").length;
    
    //For every element, loop
    for (i=0;i<XMLLength;i++)
    {
        try
        {
            //read the value of the id attribute for the current node. If it is equal to the given id, we found the index position.
            //if not, keep searching.
            if(XML.getElementsByTagName("id")[i].childNodes[0].nodeValue == ID)
            {
                nodePos = i;  
                break;
            }
        }
        catch(e)
        {
            
        }
    } 
    
    //return the node position
    return nodePos;
}

So know say you want to know what the position of a node with the ID of 5 is, you can just call
FindNodeIndex(5)
and it will return the index position in the XML for you. Of course you’ll have to tweak my function above to look in your XML, and you may want to search on some attribute other than id (just change the referenced to id to whatever you want it to be). But it works for me, so I figured I’d share.

Again, this was all something that would be very easy for a more experienced programmer, but I spend what I felt to be way to much time trying to figure this out, so I though I might be able to save someone else out there some time. Hope it helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s