Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
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 20th, 2006, 09:25 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
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 from JavaScript - not child

Code:
function loadXML()
{
    var bOneway = document.form.oneWay.value;
    var bReturn = document.forms["form"].elements["return"].value;
    var bOpenjaw = document.form.openJaw.value;

    var xmlDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
    xmlDoc.async="false";

    {
    ==================================    
        removed unnecessary code
    ==================================
    }

    // If the XML document has been completely loaded then proceed 
    if (xmlDoc.readyState==4)
    {
        xmlDoc.loadXML(document.form_dummy.optionXML.value);
        document.form_dummy.optionXML.disabled=true;
        var optNo = parseInt(document.form.optionNumber.value); 
        var optNo_OJ = parseInt(document.form.optionNumber_OJ.value); 
        var iNoOfOpts = parseInt(document.form.iNoOfOpt.value);

        // If the XML document has got options            
        if (xmlDoc.documentElement.hasChildNodes)
        {                
            var oNode_D = xmlDoc.documentElement.selectNodes("Departing").item(0).selectNodes("PiccoOption");
            var oNode_R = xmlDoc.documentElement.selectNodes("Returning").item(0).selectNodes("PiccoOption");                

            var iNoOfDep = parseInt(xmlDoc.documentElement.selectNodes("Departing").item(0).selectNodes("PiccoOption").length);
            var iNoOfRet = parseInt(xmlDoc.documentElement.selectNodes("Returning").item(0).selectNodes("PiccoOption").length);

            // Loop through all the options that have been returned from the database
            for(var i=0; i<iNoOfDep; i++)
            {
                // if the current option is not the option that was selected
                // then go ahead and delete it from the document
                if (i!=optNo)
                {
                    xmlDoc.firstChild.firstChild.selectNodes("Departing").removeChild(oNode_D.item(i));
                }
            }

            // Loop through all the options that have been returned from the database
            for(var i=0; i<iNoOfRet; i++)
            {
                // if the current option is not the option that was selected
                // then go ahead and delete it from the document
                if (i!=optNo_OJ)
                {
                    xmlDoc.firstChild.selectNodes("Returning").removeChild(oNode_R.item(i));
                }
            }
        }
    document.form.optionXML_rev.value=(xmlDoc.xml); // Write the XML to the form field text box to be submitted through to NF2
    }
}
This is my XML Document (example):

Code:
<PiccoReply>
    <Departing>
        <PiccoOption> <-- delete some of these nodes and their childnodes
            ~Other Nodes in here~
        </PiccoOption>
        <PiccoOption> 
            ~Other Nodes in here~
        </PiccoOption>
        <PiccoOption>
            ~Other Nodes in here~
        </PiccoOption>
        <PiccoOption>
            ~Other Nodes in here~
        </PiccoOption>
    </Departing>
    <Returning>
        <PiccoOption> <-- delete some of these nodes and their childnodes
            ~Other Nodes in here~
        </PiccoOption>
        <PiccoOption>
            ~Other Nodes in here~
        </PiccoOption>
        <PiccoOption>
            ~Other Nodes in here~
        </PiccoOption>
        <PiccoOption>
            ~Other Nodes in here~
        </PiccoOption>
    </Returning>
</PiccoReply>
Basically I am trying to delete some of the picco option nodes in both the 'Departing' and 'Returning' nodes. All options should be deleted
except the one that i selected.

However, i am getting an error on this line:
xmlDoc.firstChild.firstChild.selectNodes("Departin g").removeChild(oNode_D.item(i));
"Object doesn't support this property or method"

Any help appreciated.

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 27th, 2006, 11:42 AM
Authorized User
 
Join Date: Mar 2006
Location: Chennai, Tamil Nadu, India.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Raghunathan
Default

Sir,

xmlDoc.firstChild.firstChild.selectNodes("Departin g").removeChild(oNode_D.item(i));

In this line, as you've been using the selectNodes method the error has occured,
as selectNodes method returns an ARRAY of matching nodes.

so you've iterate thru the array and delete the childNodes by calling the removeChild
or
you can use the selectSingleNode method instead of selectNodes, if you're sure that
there'll only one <DEPARTING> Node in the xml file.

hope this will help your need.
do revert back

thanks n


Regards,
Raghu




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
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
delete XML node in Perl crmpicco Perl 4 November 28th, 2006 12:12 PM
finished javascript function to delete XML node crmpicco Javascript 1 April 20th, 2006 11:19 AM
delete XML node with javascript crmpicco Javascript How-To 5 April 6th, 2006 03:35 AM





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