Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 5th, 2006, 08:24 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default delete XML node with javascript

Is there a way to delete XML node with javascript?

Picco

www.crmpicco.co.uk
www.ie7.com
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old April 5th, 2006, 08:35 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

Code:
function loadXML(xmlFile)
{
    var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    xmlDoc.async="false";
    xmlDoc.loadXML(document.form.optionXML.value);
    //alert(xmlDoc.documentElement.xml)

    var optNo = parseFloat(document.form.optionNumber.value);

    //alert("optNo = " + optNo);
    //alert("xmlDoc.documentElement.hasChildnodes = " + xmlDoc.documentElement.hasChildNodes);

    if (xmlDoc.documentElement.hasChildNodes)
    {
        //alert(xmlDoc.documentElement.selectNodes("PiccoOption").length);
        var iNoOfOpts = parseFloat(xmlDoc.documentElement.selectNodes("PiccoOption").length);

        for (i=0; i<iNoOfOpts; i++)
        {
            //alert(xmlDoc.documentElement.selectNodes("PiccoOption").item(0).selectNodes("ContractDetails").item(0).selectNodes("JourneyType").item(0).text);
            if (i!=optNo)
            {
                xmlDoc.documentElement.removeChild(xmlDoc.documentElement.childNodes.item(i));
            }
        }
    }    
}
this code i have doesnt work ATM

www.crmpicco.co.uk
www.ie7.com
 
Old April 5th, 2006, 08:41 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Can you show the XML that is in document.form.optionXML.value?
What does document.form.optionNumber.value contain?
What do you expect the code to do, you are trying to remove nodes but are not doing anything with the new document?

--

Joe (Microsoft MVP - XML)
 
Old April 5th, 2006, 08:54 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

Code:
<test><picco><range>111</range></picco><picco><range>222</range></picco><picco><range>333</range></picco></test>
that is an example of what is it, with the node names changed...

i was trying to delete the nodes before, i then use the document again.

how can this be done?

optionNumber contains an integer (e.g. 2)

www.crmpicco.co.uk
www.ie7.com
 
Old April 5th, 2006, 11:05 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

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>

www.crmpicco.co.uk
www.ie7.com
 
Old April 6th, 2006, 03:35 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

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);
}
that is the function as it stands, it is still giving an error. any ideas guys?

www.crmpicco.co.uk
www.ie7.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete a xml node with all attributes and s vishnu108mishra XML 2 November 14th, 2007 04:19 AM
delete XML node in Perl crmpicco Perl 4 November 28th, 2006 12:12 PM
delete XML node from JavaScript - not child crmpicco Javascript How-To 1 April 27th, 2006 11:42 AM
finished javascript function to delete XML node crmpicco Javascript 1 April 20th, 2006 11:19 AM
how to add or delete a node from an xml documnet sai XML 1 March 22nd, 2004 06:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.