使用asp遍历XML文档

时间:2011-09-28 09:00:14

标签: xml asp-classic

我正在尝试查找XML文件中是否存在某个元素,如果存在,请将其删除。但是我一直收到这个错误:

Description: Type mismatch: 'NodeList'

我的代码如下所示:

<%@ 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")

%>

XML文件如下所示:

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

表单是一个下拉菜单,其中填充了相同的XML文件。 有谁知道可能导致此错误的原因?

1 个答案:

答案 0 :(得分:2)

看起来你的循环中有几个错误。试试这个:

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