Connecting Tech Pros Worldwide Forums | Help | Site Map

delete/remove node from XML with JavaScript

crmpicco@aol.com
Guest
 
Posts: n/a
#1: Apr 5 '06
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.
Expand|Select|Wrap|Line Numbers
  1. function loadXML()
  2. {
  3. var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
  4. xmlDoc.async="false";
  5. xmlDoc.loadXML(document.form.optionXML.value); // The XML file (e.g.
  6. '<PiccoReply><PiccoOption>....etc......')
  7.  
  8. var optNo = parseFloat(document.form.optionNumber.value); // An
  9. integer
  10.  
  11. if (xmlDoc.documentElement.hasChildNodes)
  12. {
  13. var iNoOfOpts =
  14. parseFloat(xmlDoc.documentElement.selectNodes("PiccoOption").length);
  15.  
  16. for (i=0; i<iNoOfOpts; i++)
  17. {
  18. if (i!=optNo)
  19. {
  20. alert("Ref = " +
  21. xmlDoc.documentElement.selectNodes("PiccoOption").item(i).selectNodes("Details").item(0).selectNodes("Ref").item(0).text);
  22. alert("i = " + i);
  23. alert("optNo = " + optNo);
  24.  
  25. var delNode = xmlDoc.documentElement.childNodes
  26. delNode.removeChild(true);
  27. }
  28. }
  29. }
  30. }
  31.  

My XML document:
Expand|Select|Wrap|Line Numbers
  1. <PiccoReply>
  2. <PiccoOption>
  3. <Details>
  4. <JourneyType>RETURN</JourneyType>
  5. <Ref>59</Ref>
  6. </Details>
  7. </PiccoOption>
  8. <PiccoOption>
  9. <Details>
  10. <JourneyType>GLASGOW</JourneyType>
  11. <Ref>9</Ref>
  12. </Details>
  13. </PiccoOption>
  14. <PiccoOption>
  15. <Details>
  16. <JourneyType>IRVINE</JourneyType>
  17. <Ref>9</Ref>
  18. </Details>
  19. </PiccoOption>
  20. </PiccoReply>
  21.  

Ronaldo Junior
Guest
 
Posts: n/a
#2: Apr 5 '06

re: delete/remove node from XML with JavaScript


crmpicco@aol.com wrote:[color=blue]
> 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);[/color]

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.

addi
Guest
 
Posts: n/a
#3: Apr 5 '06

re: delete/remove node from XML with JavaScript


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.

crmpicco@aol.com
Guest
 
Posts: n/a
#4: Apr 6 '06

re: delete/remove node from XML with JavaScript


Expand|Select|Wrap|Line Numbers
  1. function loadXML()
  2. {
  3. var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
  4. xmlDoc.async="false";
  5. xmlDoc.loadXML(document.form.optionXML.value);
  6. var optNo = parseInt(document.form.optionNumber.value); // No need to
  7. make a Float out of this. Integer will suffice.
  8.  
  9. if (xmlDoc.documentElement.hasChildNodes)
  10. {
  11. var nodes =
  12. xmlDoc.documentElement.getElementsByTagName("VolaroOption") // Let's
  13. make the reference a bit shorter and use an easier method.
  14. var i=0
  15. while(nodes.item(i))
  16. {
  17. if (i!=optNo)
  18. {
  19. //alert("Ref = " +
  20. nodes.item(i).getElementsByTagName("Details").item(0).getElementsByTagName("Ref").item(0).text);
  21. //alert("i = " + i);
  22. //alert("optNo = " + optNo);
  23.  
  24. xmlDoc.documentElement.childNodes.removeChild(xmlDoc.documentElement.getElementsByTagName("VolaroOption"));
  25. // removeChild takes the child to remove as an argument
  26. }
  27. else
  28. {
  29. i++ // We only increment i if we have found the node(s) we want to
  30. keep, since the item method will return different children with the
  31. same index if we modify the "nodes" collection.
  32. }
  33. }
  34. }
  35. //alert(xmlDoc.xml);
  36. //document.form.optionXML.value=(xmlDoc.xml);
  37. }
  38.  
I tried your suggestion, but it doesnt seem to work.

Quote:
parentNodeObject.removeChild(childNodeObjectToRemo ve);
crmpicco@aol.com
Guest
 
Posts: n/a
#5: Apr 6 '06

re: delete/remove node from XML with JavaScript


Expand|Select|Wrap|Line Numbers
  1. function loadXML()
  2. {
  3. var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
  4. xmlDoc.async="false";
  5. xmlDoc.loadXML(document.form.optionXML.value);
  6. var optNo = parseInt(document.form.optionNumber.value); // No need to
  7. make a Float out of this. Integer will suffice.
  8.  
  9. if (xmlDoc.documentElement.hasChildNodes)
  10. {
  11. var nodes =
  12. xmlDoc.documentElement.getElementsByTagName("PiccoOption") // Let's
  13. make the reference a bit shorter and use an easier method.
  14. var i=0
  15. while(nodes.item(i))
  16. {
  17. if (i!=optNo)
  18. {
  19. //alert("Ref = " +
  20. nodes.item(i).getElementsByTagName("Details").item(0).getElementsByTagName("Ref").item(0).text);
  21. //alert("i = " + i);
  22. //alert("optNo = " + optNo);
  23.  
  24. //xmlDoc.documentElement.childNodes.removeChild(xmlDoc.documentElement.childNodes.getElementsByTagName("VolaroOption"));
  25. // removeChild takes the child to remove as an argument
  26. if (xmlDoc.documentElement.childNodes.item(i)!=null)
  27. {
  28. xmlDoc.documentElement.removeChild(xmlDoc.documentElement.childNodes.item(i));
  29. alert("deleted node");
  30. }
  31. }
  32. else
  33. {
  34. i++ // We only increment i if we have found the node(s) we want to
  35. keep, since the item method will return different children with the
  36. same index if we modify the "nodes" collection.
  37. }
  38. }
  39. }
  40. //alert(xmlDoc.xml);
  41. //document.form.optionXML.value=(xmlDoc.xml);
  42. }
  43.  
this is my function as it is sitting ATM, still seems to give an
error...

any ideas?

Closed Thread


Similar JavaScript / Ajax / DHTML bytes