-1

Having a small issue here, I am working on a legacy Classic ASP application for a customer and have the need to pull data from an XML file but I cannot get the value of a single node, my code just returns nothing.

I have and am able to pull all data from the XML file without any problems.

XML file

<?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 file

<%
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 
%>

The value = objNode.text simply returns nothing and if I move back along the XPath I still get nothing.

It's late and I know I am missing something simple ?!?

Cheers,

Ozzie

UPDATE: OK a little update here, if i do the following:

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

I get the required values, the issue is there could be upto 24 sensors linked and the ROMId is the unique identifier so we really need to filter on this so the issue must be to do with the XPATH string I think.

OPSL
  • 131
  • 1
  • 11
  • @har07 Node doesn't have an `InnerText` property your thinking of `objNode.Value`. – user692942 Aug 16 '14 at 23:32
  • What does "if I move back along the XPath I still get nothing" mean? objNode is not nothing, correct? But you mean e.g. the XPath `/Devices-Detail-Response` returns a non-null objNode and its .text property returns nothing? – LarsH Aug 17 '14 at 02:08
  • @Lankymart `objNode.Value` did not return anything either, although I am not saying it's wrong :-) @LarsH What I mean is if I remove the later parts of the XPath string and work backwards I would expect something to be returned from further up the tree?? If I place `response.write(objXML.xml)` directly after `objXML.load(strXMLFile)` I get the full output of the XML file so I know we are getting to read and load the file so I guess the issue then must either lie in the XPath string or the objNode portion but I cannot figure out where as it really looks OK to me :-( – OPSL Aug 17 '14 at 07:03

2 Answers2

1

I think you are missing an ElseIf condition in your replace block.

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
Flakes
  • 2,422
  • 8
  • 28
  • 32
1

Given the pointer that @SearchAndResQ gave above I modified the function as follows:

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

Note that I have removed the string replacement section and hard coded the single quotes into the XPath string.

This now provides exactly the data needed even with multiple sensors attached. :-) thank you all

OPSL
  • 131
  • 1
  • 11