Reputation: 1267
Here is my XML file:
<?xml version="1.0" encoding="iso-8859-1" ?>
<data>
<metadata>
<sector>weather</sector>
<title>sourceTitle</title>
</metadata>
<weather>
<countries>
<country code="AU" name="Australia" region="Oceania">
<location type="APLOC" code="6700" name="Addington" state="VIC" postcode="3352">
<point_forecasts type="TWC">
<related_location type="TWCID" code="9508" name="Ballarat" state="VIC">
</related_location>
<point_forecast time="2015-06-22T09:00:00" tz="EST" utc_time="2015-06-21T23:00:00">
<temp_c units="°C">4</temp_c>
<dp_c units="°C">3</dp_c>
<rh units="%">91</rh>
<uv index="99"></uv>
</point_forecast>
</point_forecasts>
</location>
<location type="APLOC" code="14608" name="Albany" state="WA" postcode="6330">
<point_forecasts type="TWC">
<related_location type="TWCID" code="9541" name="Albany" state="WA">
</related_location>
<point_forecast time="2015-06-22T07:00:00" tz="WST" utc_time="2015-06-21T23:00:00">
<temp_c units="°C">10</temp_c>
<dp_c units="°C">7</dp_c>
<rh units="%">83</rh>
</point_forecast>
</point_forecasts>
</location>
<location type="APLOC" code="4205" name="Albury" state="NSW" postcode="2640">
<point_forecasts type="TWC">
<related_location type="TWCID" code="9074" name="Albury" state="NSW">
</related_location>
<point_forecast time="2015-06-22T09:00:00" tz="EST" utc_time="2015-06-21T23:00:00">
<temp_c units="°C">4</temp_c>
<dp_c units="°C">3</dp_c>
<rh units="%">97</rh>
<uv index="88"></uv>
</point_forecast>
</point_forecasts>
</location>
</country>
</countries>
</weather>
</data>
and here is my VBScript:
Dim url, objxml
url = "http://bayerwebsitesdev.ap.bayer.cnb/bl/prosaro/can.xml"
Set objxml = CreateObject("Msxml2.DOMDocument")
objxml.setProperty "SelectionLanguage", "XPath"
objXML.async = False
objXML.Load url
dim basePath, nLocation, temp_c,dp_c, rh , uv, icon
dim nPostCode, nPoint_forecast, nTime, nTz, uvTest
basePath = "data/weather/countries/country/location/point_forecasts/point_forecast/"
set temp_c = objxml.getElementsByTagName(basePath & "temp_c")
set dp_c = objxml.getElementsByTagName(basePath & "dp_c")
set rh = objxml.getElementsByTagName(basePath & "rh")
set uv = objxml.getElementsByTagName(basePath & "uv")
for each nLocation in objxml.SelectNodes("//location")
nPostCode = nLocation.getAttribute("postcode")
writeLog "pCode = " & nPostCode
for each nPoint_forecast In nLocation.SelectNodes("*/point_forecast")
nTime = nPoint_forecast.getAttribute("time")
nTz = nPoint_forecast.getAttribute("tz")
writeLog "time = " & nTime & " - tz = " & nTz
Set uvTest = nPoint_forecast.SelectSingleNodes("*/uv")
If Not uvTest Is Nothing Then
writeLog uvTest.item(0).Text
end if
'Set tempTest = objxml.documentElement.selectSingleNode("/data/weather/countries/country/location/point_forecasts/point_forecast/temp_c")
'writeLog "--temTest :" & tempTest.text
next
next
Is using uvTest.item(0).Text
the best approach here? When I print out the second lot of data (postcode="6330"
) it actually shows the uv
result (88) for the third postcode (postcode="2640"
). How can I stop the index jumping to the next uvTest.item(0)
point_forecast
result if its missing?
Upvotes: 0
Views: 65
Reputation: 38745
Is using uvTest.item(0).Text the best approach here?
No, because the uv nodes have neither items nor text:
<uv index="88"></uv>
Concentrating on postalcode and index:
Option Explicit
Dim objxml : Set objxml = CreateObject("Msxml2.DOMDocument")
objxml.setProperty "SelectionLanguage", "XPath"
objXML.async = False
objXML.Load "..\data\30973938.xml"
If 0 = objXML.ParseError Then
Dim sXPath : sXPath = "/data/weather/countries/country/location"
Dim ndlLoc : Set ndlLoc = objXML.selectNodes(sXpath)
If 0 < ndlLoc.length Then
Dim ndLoc
For Each ndLoc In ndlLoc
Dim sUv : sUv = "no uv"
Dim ndUv : Set ndUv = ndLoc.selectSingleNode("point_forecasts/point_forecast/uv")
If Not ndUv Is Nothing Then
'sUv = ndUv.item(0).Text - Object doesn't support this property or method: 'item'
sUv = ndUv.getAttribute("index")
End If
WScript.Echo ndLoc.getAttribute("postcode"), sUv
Next
Else
WScript.Echo "not found |" & sXPath & "|"
End If
Else
WScript.Echo objXML.ParseError.Reason
End If
output:
cscript 30973938.vbs
3352 99
6330 no uv
2640 88
Upvotes: 1
Reputation: 200193
The code you posted can't possibly work with the XML you posted.
SelectSingleNodes
. It's either SelectNodes
or SelectSingleNode
.<uv>
nodes are immediate children of the <point_forecast>
nodes. An XPath expression */uv
would match grandchildren (like with */point_forecast
).index
is not the text
(content) of the node.To get the index value of the <uv>
child node of the current <point_forecast>
node change this:
Set uvTest = nPoint_forecast.SelectSingleNodes("*/uv")
If Not uvTest Is Nothing Then
writeLog uvTest.item(0).Text
end if
into this:
Set uvTest = nPoint_forecast.SelectSingleNode("uv")
If Not uvTest Is Nothing Then
writeLog uvTest.getAttribute("index")
End If
Upvotes: 0