选择跳过缺少的childnode节点

时间:2015-06-22 07:10:59

标签: xpath vbscript xml-parsing

这是我的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="&#176;C">4</temp_c>
        <dp_c units="&#176;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="&#176;C">10</temp_c>
        <dp_c units="&#176;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="&#176;C">4</temp_c>
        <dp_c units="&#176;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")时,它实际上显示了第三个邮政编码(uv)的postcode="2640"结果(88)。如果索引丢失,我怎样才能阻止索引跳转到下一个uvTest.item(0) point_forecast结果?

2 个答案:

答案 0 :(得分:1)

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 "..\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

输出:

cscript 30973938.vbs
3352 99
6330 no uv
2640 88

答案 1 :(得分:0)

您发布的代码无法使用您发布的XML。

  • 没有方法SelectSingleNodes。它可以是SelectNodesSelectSingleNode
  • <uv>个节点是<point_forecast>个节点的直接子节点。 XPath表达式*/uv将匹配孙子(与*/point_forecast一样)。
  • 属性index不是节点的text(内容)。

要获取当前<uv>节点的<point_forecast>子节点的索引值,请更改此项:

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