Traverse XML document using asp

北战南征 提交于 2020-01-05 07:50:12

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/7580721/traverse-xml-document-using-asp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!