Jump to content

XMLDOM question


Recommended Posts

Guys,I'm having some trouble with the removeChild() method and I was wondering if I could get any help.I have some xml as follows:

<products><product> <id>product1</id> <description>Product type 1</description> <price>100</price></product><product> <id>product2</id> <description>Product type 2</description> <price>100</price></product><product> <id>product3</id> <description>Product type 3</description> <price>100</price></product></products>

When I try to use the removeChild method (as follows) I get an error:

xmldoc=new ActiveXObject("Microsoft.XMLDOM")xmldoc.async="false"xmldoc.load("products.xml")node = xmldoc.documentElement.selectSingleNode("Products")nodename = "product[id='product" + node.childNodes.length +"']"node.removeChild(nodename)

Can someone tell me why the removeChild() method is generating an error?I think it has something to do with the way I've idenitified the products, but I can't see what's wrong with the code.thanks in advance.Dooberry :)

Link to comment
Share on other sites

I fixed it.According to the w3 tutorial the child node should be the name of the node that needs to be removed - I assumed that this meant the string which represents the name.In actual fact, the removeChild() method needs to have the node object passed to it.in this case:

rootel = xmldoc.documentElement.selectSingleNode("Products")if (rootel.childNodes.length){removenode = rootel.selectSingleNode("product[id='product" + rootel.childNodes.length + "']")rootel.removeChild(removenode)}

I could have reduced the number of steps in the code by writing:

if (rootel.childNodes.length){rootel.removeChild(rootel.selectSingleNode("product[id='product" + rootel.childNodes.length + "']"))}

but that makes the code less readable!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...