delete/remove node from XML with JavaScript

C

crmpicco

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>
 
R

Ronaldo Junior

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.
 
A

addi

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.
 
C

crmpicco

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.
 
C

crmpicco

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top