delete/remove node from XML with JavaScript

Discussion in 'Javascript' started by crmpicco@aol.com, Apr 5, 2006.

  1. Guest

    I am trying to delete all 'PiccoOption' nodes that i do not need, i
    then want to put the document, with just one 'PiccoOption' into the
    form field 'document.form.optionNumber.value'.
    This code doesnt seem to work.....
    Any help appreciated.
    Code:
    function loadXML()
    {
    var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    xmlDoc.async="false";
    xmlDoc.loadXML(document.form.optionXML.value); // The XML file (e.g.
    '<PiccoReply><PiccoOption>....etc......')
    
    var optNo = parseFloat(document.form.optionNumber.value); // An
    integer
    
    if (xmlDoc.documentElement.hasChildNodes)
    {
    var iNoOfOpts =
    parseFloat(xmlDoc.documentElement.selectNodes("PiccoOption").length);
    
    for (i=0; i<iNoOfOpts; i++)
    {
    if (i!=optNo)
    {
    alert("Ref = " +
    xmlDoc.documentElement.selectNodes("PiccoOption").item(i).selectNodes("Details").item(0).selectNodes("Ref").item(0).text);
    alert("i = " + i);
    alert("optNo = " + optNo);
    
    var delNode = xmlDoc.documentElement.childNodes
    delNode.removeChild(true);
    }
    }
    }
    }
    

    My XML document:
    Code:
    <PiccoReply>
    <PiccoOption>
    <Details>
    <JourneyType>RETURN</JourneyType>
    <Ref>59</Ref>
    </Details>
    </PiccoOption>
    <PiccoOption>
    <Details>
    <JourneyType>GLASGOW</JourneyType>
    <Ref>9</Ref>
    </Details>
    </PiccoOption>
    <PiccoOption>
    <Details>
    <JourneyType>IRVINE</JourneyType>
    <Ref>9</Ref>
    </Details>
    </PiccoOption>
    </PiccoReply>
    
     
    , Apr 5, 2006
    #1
    1. Advertising

  2. wrote:
    > I am trying to delete all 'PiccoOption' nodes that i do not need, i
    > then want to put the document, with just one 'PiccoOption' into the
    > form field 'document.form.optionNumber.value'.
    > This code doesnt seem to work.....
    > Any help appreciated.
    > [snip]
    >
    > delNode.removeChild(true);


    The XML DOM's removeChild method is slightly different than the regular
    HTML DOM one.

    Instead of "true" (to remove child nodes from the node to be deleted),
    you should pass the node to be deleted as argument.
     
    Ronaldo Junior, Apr 5, 2006
    #2
    1. Advertising

  3. addi Guest

    I'd like to recommend the use of Sarissa
    (http://sarissa.sourceforge.net/doc/), an excellent piece of
    cross-browser code for handling XML data.

    I believe that you are supposed to call "removeChild()" on the parent
    of the node you are trying to delete:

    parentNodeObject.removeChild(childNodeObjectToRemove);

    But hey, I've been wrong before.
     
    addi, Apr 5, 2006
    #3
  4. Guest

    Code:
    function loadXML()
    {
    var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    xmlDoc.async="false";
    xmlDoc.loadXML(document.form.optionXML.value);
    var optNo = parseInt(document.form.optionNumber.value); // No need to
    make a Float out of this. Integer will suffice.
    
    if (xmlDoc.documentElement.hasChildNodes)
    {
    var nodes =
    xmlDoc.documentElement.getElementsByTagName("VolaroOption") // Let's
    make the reference a bit shorter and use an easier method.
    var i=0
    while(nodes.item(i))
    {
    if (i!=optNo)
    {
    //alert("Ref = " +
    nodes.item(i).getElementsByTagName("Details").item(0).getElementsByTagName("Ref").item(0).text);
    //alert("i = " + i);
    //alert("optNo = " + optNo);
    
    xmlDoc.documentElement.childNodes.removeChild(xmlDoc.documentElement.getElementsByTagName("VolaroOption"));
    // removeChild takes the child to remove as an argument
    }
    else
    {
    i++ // We only increment i if we have found the node(s) we want to
    keep, since the item method will return different children with the
    same index if we modify the "nodes" collection.
    }
    }
    }
    //alert(xmlDoc.xml);
    //document.form.optionXML.value=(xmlDoc.xml);
    }
    
    I tried your suggestion, but it doesnt seem to work.

     
    , Apr 6, 2006
    #4
  5. Guest

    Code:
    function loadXML()
    {
    var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    xmlDoc.async="false";
    xmlDoc.loadXML(document.form.optionXML.value);
    var optNo = parseInt(document.form.optionNumber.value); // No need to
    make a Float out of this. Integer will suffice.
    
    if (xmlDoc.documentElement.hasChildNodes)
    {
    var nodes =
    xmlDoc.documentElement.getElementsByTagName("PiccoOption") // Let's
    make the reference a bit shorter and use an easier method.
    var i=0
    while(nodes.item(i))
    {
    if (i!=optNo)
    {
    //alert("Ref = " +
    nodes.item(i).getElementsByTagName("Details").item(0).getElementsByTagName("Ref").item(0).text);
    //alert("i = " + i);
    //alert("optNo = " + optNo);
    
    //xmlDoc.documentElement.childNodes.removeChild(xmlDoc.documentElement.childNodes.getElementsByTagName("VolaroOption"));
    // removeChild takes the child to remove as an argument
    if (xmlDoc.documentElement.childNodes.item(i)!=null)
    {
    xmlDoc.documentElement.removeChild(xmlDoc.documentElement.childNodes.item(i));
    alert("deleted node");
    }
    }
    else
    {
    i++ // We only increment i if we have found the node(s) we want to
    keep, since the item method will return different children with the
    same index if we modify the "nodes" collection.
    }
    }
    }
    //alert(xmlDoc.xml);
    //document.form.optionXML.value=(xmlDoc.xml);
    }
    
    this is my function as it is sitting ATM, still seems to give an
    error...

    any ideas?
     
    , Apr 6, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,668
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,520
    Dimitre Novatchev
    Aug 24, 2006
  3. sangram
    Replies:
    16
    Views:
    2,086
  4. Martin Honnen

    Re: Reading XML Node-by-Node

    Martin Honnen, Nov 12, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    408
    Martin Honnen
    Nov 12, 2008
  5. Geagleeye

    how to creating new node inside a xml node

    Geagleeye, Aug 2, 2006, in forum: ASP General
    Replies:
    2
    Views:
    219
    Geagleeye
    Aug 3, 2006
Loading...

Share This Page