1

I'm trying to find if a certain element is present in a XML file and if it is, remove it. However I keep getting this error:

Description: Type mismatch: 'NodeList'

My code looks like this:

<%@ Language=VbScript%>
<%

Dim address

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.load (Server.MapPath("XML/sensor.xml"))

Set Root = XMLDoc.documentElement 

Set NodeList = Root.getElementsByTagName("sensor")

For Each i In NodeList

    if ((NodeList(i).getElementsByTagName("Address")(0).childNodes(0).nodeValue)=request.form("remove_address")) then
    NodeList.parentNode.removeChild NodeList 
    End if
Next



NodeList.parentNode.removeChild NodeList 
xmlDoc.Save "\www./XML/sensores.xml"

Response.Redirect("remove_sensor_modbus.html")

%>

And the XML file looks like this:

<?xml version="1.0"?>
<sensors>
<sensor>
    <Address>40000</Address>
</sensor>
<sensor>
    <Address>46999</Address>
</sensor>
</sensors>

The form is a dropdown menu populated with this same XML file. Does anyone knows what could be causing this error?

Jamiro14
  • 203
  • 4
  • 12
  • Is this the complete XML? If so, you're missing the closing `` element. Also, why are you using traditional ASP? If you have access to ASP.NET, I recommend you to use that instead. – Asbjørn Ulsberg Sep 28 '11 at 09:03
  • The XmL was complete but it wasn't correctly displayed. Don't know why. I would use ASP.NET if I could, but I'm stuck with a legacy webserver that only supports classic ASP. – Jamiro14 Sep 28 '11 at 10:04

1 Answers1

2

It looks like you have several errors in your loop. Try this:

Dim removeAddress
Set removeAddress = Request.Form("remove_address")

For Each sensorNode In NodeList
    Dim addressNode
    Set addressNode = sensorNode.GetElementsByTagName("Address")(0)

    If (addressNode.Text = removeAddress) Then
        sensorNode.RemoveChild(addressNode)
    End if
Next
Asbjørn Ulsberg
  • 8,721
  • 3
  • 45
  • 61
  • 1
    Thank you. Just had to do a minor change instead of "sensorNode.RemoveChild(addressNode)" i used "sensorNode.parentNode.removeChild sensorNode" because it just removed the text leaving an empty node, that caused the script to crash if it was used more than once. – Jamiro14 Sep 28 '11 at 10:20