Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > delete/remove node from XML with JavaScript

Reply
Thread Tools

delete/remove node from XML with JavaScript

 
 
crmpicco@aol.com
Guest
Posts: n/a
 
      04-05-2006
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>
 
Reply With Quote
 
 
 
 
Ronaldo Junior
Guest
Posts: n/a
 
      04-05-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
addi
Guest
Posts: n/a
 
      04-05-2006
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(childNodeObjectToRemo ve);

But hey, I've been wrong before.

 
Reply With Quote
 
crmpicco@aol.com
Guest
Posts: n/a
 
      04-06-2006
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.

Quote:
parentNodeObject.removeChild(childNodeObjectToRemo ve);
 
Reply With Quote
 
crmpicco@aol.com
Guest
Posts: n/a
 
      04-06-2006
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Reading XML Node-by-Node Martin Honnen ASP .Net 0 11-12-2008 06:29 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
how to creating new node inside a xml node Geagleeye ASP General 2 08-03-2006 12:09 PM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
How to drag a sub-node to another node in TreeView? wolf ASP .Net 0 08-18-2004 11:51 PM



Advertisments