VBScript从XML节点分配变量

时间:2014-09-04 19:07:42

标签: xml xpath vbscript

我正在尝试编写一个简单的VBScript,它查看XML文件,提取两个属性,将它们添加到结果输出文件中。到目前为止,我已经能够使用以下方法加载XML输入文件:

Option Explicit
Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Load "C:\_STATS_SCRIPTS\STATS_HOME.xml"

然后我创建并初始化了这样的变量:

Dim fumlost
Dim intercept
Dim turnovers
fumlost="0"
intercept="0"
turnovers="0"

以下是我的XML的样子(在总数子树之后截断):

<fbgame source="Tas Football" version="4.16.01" generated="9/3/2014">
    <venue gameid="01UA-WVU" visid="WVU" homeid="UA" visname="West Virginia" homename="Alabama" date="8/30/2014" location="Atlanta, Georgia" stadium="Georgia Dome" start="3:36" end="7:05" neutralgame="Y" duration="3:29" attend="70502" temp="" wind="" weather="Indoor">
        <officials ref="David Epperly" ump="Mike Webster" line="Steve Clein" lj="Rod Pearson" bj="Pat Ryan" fj="Mike Culler" sj="Eddie Bonet"></officials>
        <notes>
            <note text="Replay: Dan Post"></note>
        </notes>
        <rules qtrs="4" mins="15" downs="4" yds="10" kospot="35" tbspot="20" kotbspot="25" patspot="3" safspot="20" td="6" fg="3" pat="1" patx="2" saf="2" defpat="2" rouge="1" field="100" toh="3" sackrush="Y" fgaplay="Y" netpunttb="Y"></rules>
    </venue>
    <team vh="H" code="8" id="UA" name="Alabama" record="1-0" abb="A">
        <linescore prds="4" line="3,17,10,3" score="33">
            <lineprd prd="1" score="3"></lineprd>
            <lineprd prd="2" score="17"></lineprd>
            <lineprd prd="3" score="10"></lineprd>
            <lineprd prd="4" score="3"></lineprd>
        </linescore>
        <totals totoff_plays="82" totoff_yards="538" totoff_avg="6.6">
            <firstdowns no="30" rush="13" pass="14" penalty="3"></firstdowns>
            <penalties no="7" yds="49"></penalties>
            <conversions thirdconv="9" thirdatt="15" fourthconv="0" fourthatt="1"></conversions>
            <fumbles no="0" lost="0"></fumbles>
            <misc yds="0" top="37:47" ona="0" onm="0" ptsto="0"></misc>
            <redzone att="4" scores="4" points="24" tdrush="3" tdpass="0" fgmade="1" endfga="0" enddowns="0" endint="0" endfumb="0" endhalf="0" endgame="0"></redzone>
            <rush att="49" yds="288" gain="294" loss="6" td="3" long="26"></rush>
            <pass comp="24" att="33" int="1" yds="250" td="0" long="38" sacks="0" sackyds="0"></pass>
            <rcv no="24" yds="250" td="0" long="38"></rcv>
            <punt no="2" yds="101" long="62" blkd="0" tb="0" fc="1" plus50="1" inside20="1" avg="50.5"></punt>
            <ko no="7" yds="453" ob="0" tb="3"></ko>
            <fg made="4" att="4" long="47" blkd="0"></fg>
            <pat kickatt="3" kickmade="3"></pat>
            <defense tackua="34" tacka="38" tot_tack="72" tflua="6" tfla="0" tflyds="30" sacks="3" sackyds="25" brup="3"></defense>
            <kr no="4" yds="99" td="0" long="26"></kr>
            <pr no="1" yds="-1" td="0" long="0"></pr>
            <scoring td="3" fg="4" patkick="3"></scoring>
        </totals>

我接下来需要做的是将/ fbgame / team / totals / fumbles @fold分配给我的fumbles变量,将/ fbgame / team / totals / pass @ int分配给我的拦截变量,然后将两者一起添加到make失误,然后输出。我想我可以处理变量的总和并输出一个文件,但我对如何将XML属性分配给我的变量感到很遗憾。之前我成功制作了一个脚本,使用sXPath将访问团队和主队从我的主输入文件中分离出来,但我目前无法使用我在那里学到的东西来完成这项任务!

我非常感谢任何来自我的帮助,因为我是一名n00b脚本编写者,并且在我的头脑中!

3 个答案:

答案 0 :(得分:1)

不确定在VBScript中使用XPath,但是XPath适用于我:

string(//fbgame/team/totals/fumbles/@lost)

结果:0

string(//fbgame/team/totals/pass/@int)

结果:1​​

也许这适用于您的方法,或者您可以进一步调整。

如果您需要整个节点而不仅仅是值,请遵循XPath

//fbgame/team/totals/fumbles[@lost]

结果

 <fumbles no="0" lost="0" />

为了提供完整性 - 根据查询,//fbgame可以是/fbgame。我只是将你的XML部分调整为对解析有效,让XPath与fbgame匹配(例如,只包含一个游戏)。

如果问题不是关于XPath表达式而是关于如何在VBScript中获取XPath值,那么应该这样做(至少获取值,猜测你会将值存储在变量中以便稍后进行数学运算):< / p>

For Each a In objDoc.selectNodes ("//fbgame/team/totals/fumbles/@lost")
 Wscript.Echo a.text
Next

For Each b In objDoc.selectNodes ("//fbgame/team/totals/pass/@int")
 Wscript.Echo b.text
Next

答案 1 :(得分:0)

以下代码正确地提取值,但在总结到“失误”时返回的输出是&#34; 01&#34;而不只是&#34; 1&#34;。这是正确的吗?

Set colNodes=xmlDoc.selectNodes ("//fbgame/team/totals/pass/@int")

For Each objNode in colNodes
  intercept = objNode.text
  Wscript.Echo intercept 
Next

Set colNodes=xmlDoc.selectNodes ("//fbgame/team/totals/fumbles/@lost")

For Each objNode in colNodes
  fumlost = objNode.text
  Wscript.Echo fumlost
Next

turnovers = fumlost + intercept

Wscript.Echo turnovers

答案 2 :(得分:0)

感谢您的帮助!没有stackoverflow就无法做到这一点!最终工作代码:

Set xmlDoc = CreateObject("Microsoft.XMLDOM")

xmlDoc.Async = "False"
xmlDoc.Load("STATS_HOME.xml")


Dim fumlost
Dim intercept
Dim turnovers
fumlost="0"
intercept="0"
turnovers="0"

Set colNodes=xmlDoc.selectNodes ("//fbgame/team/totals/pass/@int")

For Each objNode in colNodes
  intercept = objNode.text
Next

Set colNodes=xmlDoc.selectNodes ("//fbgame/team/totals/fumbles/@lost")

For Each objNode in colNodes
  fumlost = objNode.text
Next
turnovers = CInt(fumlost) + CInt(intercept)

Dim fso
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set f = fso.OpenTextFile("C:\_STATS_SCRIPTS\TO_HOME.txt", 2)
f.Writeline turnovers