2015-06-22 69 views
0

这里是我的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="&#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")时,它实际上显示了第三个邮编(postcode="2640")的uv结果(88)。如何避免索引跳到下一个uvTest.item(0)point_forecast结果是否失踪?

回答

0

您发布的代码不可能与您发布的XML一起使用。

  • 没有方法SelectSingleNodes。它可以是SelectNodesSelectSingleNode
  • <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 
+0

谢谢。这工作得很好。其实两个非常有用的解决方案,我的问题。感谢您指出我所犯的一些明显错误。给你一个非常美好的一天! – Mat41

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 
+0

谢谢。你的代码也工作得很好。说实话,我想接受这两个,这是非常有用的两个伟大的解决方案!你有点更详细,但安斯加第一。给你一个非常美好的一天! – Mat41