在这里遇到一个小问题,我正在为客户开发一个传统的经典ASP应用程序,并且需要从XML文件中提取数据但是我无法获得单个节点的值,我的代码只返回任何内容。 / p>
我已经能够从XML文件中提取所有数据而没有任何问题。
XML文件
<?xml version="1.0" encoding="UTF-8"?>
<Devices-Detail-Response>
<PollCount>36593</PollCount>
<DevicesConnected>1</DevicesConnected>
<LoopTime>1.031</LoopTime>
<DataErrors>0</DataErrors>
<DeviceName>OW_SERVER-Enet</DeviceName>
<HostName>EDSOWSERVER</HostName>
<MACAddress>00:50:C2:91:B3:9C</MACAddress>
<owd_DS18B20 Description="Programmable resolution thermometer">
<Name>DS18B20</Name>
<Family>28</Family>
<ROMId>2D0000023C519A28</ROMId>
<Health>7</Health>
<RawData>61014B467FFF0F1002FF000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>22.0625 Deg C</PrimaryValue>
<Temperature Units="Centigrade">22.0625</Temperature>
<UserByte1 Writable="True">75</UserByte1>
<UserByte2 Writable="True">70</UserByte2>
<Resolution>12</Resolution>
<PowerSource>255</PowerSource>
</owd_DS18B20>
</Devices-Detail-Response>
xml-text.asp文件
<%
Option Explicit
Response.Write GetTemperature("http://192.168.1.85/details.xml", "2D0000023C519A28")
Function GetTemperature(strXMLFile, strName)
Dim objXML : Set objXML = CreateObject("Msxml2.DOMDocument.6.0")
objXML.async = False
objXML.SetProperty "SelectionLanguage", "XPath"
objXML.SetProperty "ServerHTTPRequest", True
objXML.load(strXMLFile)
''// enclose in single- or double quotes accordingly
If InStr(strName, "'") > 0 And InStr(strName, """") = 0 Then
strName = """" & strName & """"
ElseIf InStr(strName, "'") = 0 And InStr(strName, """") > 0 Then
strName = "'" & strName & "'"
Else
''// both single and double quotes in a string are unsupported
strName = "''"
End If
Dim strXPath : strXPath = "/Devices-Detail-Response/owd_DS18B20[ROMId=" & strName & "]/Temperature"
Dim objNode : Set objNode = objXML.documentElement.selectSingleNode(strXPath)
If Not objNode Is Nothing Then
value = objNode.text
End If
End Function
%>
value = objNode.text
根本不返回任何内容,如果我沿着XPath返回,我仍然什么也得不到。
已经很晚了,我知道我错过了一些简单的事情?!
干杯,
奥奇
更新:如果我执行以下操作,请在此处稍加更新:
Dim xmlSensor
For Each xmlSensor In objXML.documentElement.selectNodes("owd_DS18B20")
Dim romID : romID = xmlSensor.selectSingleNode("ROMId").text
Dim temperature : temperature = xmlSensor.selectSingleNode("Temperature").text
Response.Write Server.HTMLEncode(romID) & "<br />"
Response.Write Server.HTMLEncode(temperature) & "<br /><br />"
Next
我得到了所需的值,问题是最多可以链接24个传感器,ROMId是唯一的标识符,所以我们真的需要对此进行过滤,所以问题必须与我认为的XPATH字符串有关。
答案 0 :(得分:1)
我认为您在替换块中缺少ElseIf
条件。
If InStr(strName, "'") > 0 And InStr(strName, """") = 0 Then
strName = """" & strName & """"
ElseIf InStr(strName, "'") = 0 And InStr(strName, """") > 0 Then
strName = "'" & strName & "'"
ElseIf InStr(strName, "'") > 0 And InStr(strName, """") > 0 Then
''// both single and double quotes in a string are unsupported
strName = "''"
End If
答案 1 :(得分:1)
鉴于@SearchAndResQ给出的指针,我修改了函数,如下所示:
Function GetTemperature(strXMLFile, strName)
Dim value
Dim objXML : Set objXML = CreateObject("MSXML2.DOMDocument.3.0")
objXML.async = False
objXML.load(strXMLFile)
Dim strXPath : strXPath = "/Devices-Detail-Response/owd_DS18B20[ROMId='" & strName & "']"
Dim xmlSensor
For Each xmlSensor In objXML.documentElement.selectNodes(strXPath)
Dim romID : romID = xmlSensor.selectSingleNode("ROMId").text
Dim temperature : temperature = xmlSensor.selectSingleNode("Temperature").text
Response.Write Server.HTMLEncode(romID) & "<br />"
Response.Write Server.HTMLEncode(temperature) & "<br /><br />"
Next
End Function
请注意,我已删除了字符串替换部分,并将单引号硬编码为XPath字符串。
现在,即使连接了多个传感器,也可以提供所需的数据。 :-)谢谢大家