这里是我的XML文件:选择跳过节点上缺少childnode
<?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>
这里是我的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
是使用uvTest.item(0).Text
最好的办法吗?当我打印出第二批数据(postcode="6330"
)时,它实际上显示了第三个邮编(postcode="2640"
)的uv
结果(88)。如何避免索引跳到下一个uvTest.item(0)
point_forecast
结果是否失踪?
谢谢。这工作得很好。其实两个非常有用的解决方案,我的问题。感谢您指出我所犯的一些明显错误。给你一个非常美好的一天! – Mat41