在缺少子节点时选择跳过节点
Selection skipping node on missing childnode
这是我的 XML 文件:
<?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
结果?
您发布的代码不可能与您发布的 XML 一起工作。
- 没有方法
SelectSingleNodes
。可以是 SelectNodes
或 SelectSingleNode
。
<uv>
节点是 <point_forecast>
节点的直接子节点。 XPath 表达式 */uv
将匹配孙子(如 */point_forecast
)。
- 属性
index
不是节点的text
(内容)。
要获取当前<point_forecast>
节点的<uv>
子节点的索引值,更改为:
Set uvTest = nPoint_forecast.SelectSingleNodes("*/uv")
If Not uvTest Is Nothing Then
writeLog uvTest.item(0).Text
end if
进入这个:
Set uvTest = nPoint_forecast.SelectSingleNode("uv")
If Not uvTest Is Nothing Then
writeLog uvTest.getAttribute("index")
End If
Is using uvTest.item(0).Text the best approach here?
否,因为 uv 节点既没有项目也没有文本:
<uv index="88"></uv>
关注邮政编码和索引:
Option Explicit
Dim objxml : Set objxml = CreateObject("Msxml2.DOMDocument")
objxml.setProperty "SelectionLanguage", "XPath"
objXML.async = False
objXML.Load "..\data973938.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
输出:
cscript 30973938.vbs
3352 99
6330 no uv
2640 88
这是我的 XML 文件:
<?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
结果?
您发布的代码不可能与您发布的 XML 一起工作。
- 没有方法
SelectSingleNodes
。可以是SelectNodes
或SelectSingleNode
。 <uv>
节点是<point_forecast>
节点的直接子节点。 XPath 表达式*/uv
将匹配孙子(如*/point_forecast
)。- 属性
index
不是节点的text
(内容)。
要获取当前<point_forecast>
节点的<uv>
子节点的索引值,更改为:
Set uvTest = nPoint_forecast.SelectSingleNodes("*/uv")
If Not uvTest Is Nothing Then
writeLog uvTest.item(0).Text
end if
进入这个:
Set uvTest = nPoint_forecast.SelectSingleNode("uv")
If Not uvTest Is Nothing Then
writeLog uvTest.getAttribute("index")
End If
Is using uvTest.item(0).Text the best approach here?
否,因为 uv 节点既没有项目也没有文本:
<uv index="88"></uv>
关注邮政编码和索引:
Option Explicit
Dim objxml : Set objxml = CreateObject("Msxml2.DOMDocument")
objxml.setProperty "SelectionLanguage", "XPath"
objXML.async = False
objXML.Load "..\data973938.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
输出:
cscript 30973938.vbs
3352 99
6330 no uv
2640 88