Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript 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, 05: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 finished javascript function to delete XML node

Code:
function loadXML()
{
    var xmlDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
    xmlDoc.async="false";

    // 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 iNoOfOpts = parseInt(document.form.iNoOfOpt.value);

        // If the XML document has got options
        if (xmlDoc.documentElement.hasChildNodes)
        {
            var oNode = xmlDoc.documentElement.selectNodes("PiccoOption");

            // Loop through all the options that have been returned from the database
            for(var i=0; i<iNoOfOpts; 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.removeChild(oNode.item(i));
                }
            }
        }
        document.form.optionXML_rev.value=(xmlDoc.xml); // Write the XML to the form field text box to be submitted through to NF2
    }
}
I have created this loadXML function. Can someone tell me if using var xmlDoc = new ActiveXObject("MSXML2.DOMDocument.3.0"); is the best way to refer to it in JavaScript, should it be MSXML.Document or something else?

IE Only Code.

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 20th, 2006, 11:19 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 NAME: loadXML
Load the XML from the form field and strip out all the unnecessary nodes
-- to prevent browser cache from being reached --
*/
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";

    var optNo = parseInt(document.form.optionNumber.value); // The Selected option number from the list
    var iNoOfOpts = parseInt(document.form.iNoOfOpt.value); // The total number of options returned from the database
    document.form_dummy.optionXML.disabled=true;

    if ((bReturn=="True")||(bOneway=="True")) // For Return and Oneway journeys
    {
        // If the XML document has been completely loaded then proceed 
        if (xmlDoc.readyState==4)
        {
            xmlDoc.loadXML(document.form_dummy.optionXML.value); // LOAD THE XML FROM THE FORM FIELD TO THE SCRIPT

            // If the XML document has got options
            if (xmlDoc.documentElement.hasChildNodes)
            {
                var oNode = xmlDoc.documentElement.selectNodes("PiccoOption");

                // Loop through all the options that have been returned from the database
                for(var i=0; i<iNoOfOpts; 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.removeChild(oNode.item(i)); // DELETE THE NODE
                    }
                }
            }
            document.form.optionXML_rev.value=(xmlDoc.xml); // Write the XML to the form field text box to be submitted through to NF2
        }
    }
    else if(bOpenjaw=="True") // For Openjaw journeys
    {
        // If the XML document has been completely loaded then proceed 
        if (xmlDoc.readyState==4)
        {
            xmlDoc.loadXML(document.form_dummy.optionXML.value); // LOAD THE XML FROM THE FORM FIELD TO THE SCRIPT
            var optNo_OJ = parseInt(document.form.optionNumber_OJ.value); // The Selected option number from the list (for inward openjaw)

            // 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.removeChild(oNode_D.item(i)); // DELETE THE NODE
                    }
                }

                // 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.documentElement.childNodes(1).removeChild(oNode_R.item(i)); // DELETE THE NODE
                    }
                }

            }
            document.form.optionXML_rev.value=(xmlDoc.xml); // Write the XML to the form field text box to be submitted through to NF2
        }        
    }
}
Just wondering if anyone could see at a glance a way to speed this code up? Just a general optimisation question.

Thanks
Picco

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
delete XML node with javascript crmpicco Javascript How-To 5 April 6th, 2006 03:35 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.