2014-09-04 58 views
2

我正在尝试编写一个简单的VBScript,查看XML文件,提取两个属性,将它们添加到结果并将结果输出到输出文件。到目前为止,我已经能够使用加载XML输入文件:从XML节点分配变量的VBScript

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 /团队/总计/摸索@输给我摸索变量,分配/ fbgame /团队/总计/通过@ INT到我的拦截变量,然后添加两个t一起制作失误,然后输出。我想我可以处理变量的总结和输出文件,但是我不知道如何获取分配给我的变量的XML属性。早些时候,我成功地创建了一个脚本,它使用sXPath将我的主要输入文件中的访问团队和主队分开,但我目前无法使用我在那里学到的知识来完成此任务!

我非常感谢任何帮助,因为我是一个n00b scripter,在我的头上!

回答

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值,这应该做到这一点(至少获取值,猜测您将在后面的数学中存储值的变量):

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

不介意格式化,我猜得到它;)prob是判断结果而不知道XML,但不会CInt(a)为字符串值01格式到整数1 ..?希望这不是误解,只是认为现在是问题所在。或者你可以加起来的结果? – 2014-09-04 20:10:54

+0

我无法添加这两个变量。我认为这只是把他们放在一起,当我做“营业额=拦截+ fumlost” – 2014-09-04 20:16:31

0

下面的代码正确拉动值,但是当总结'失误'时,返回的输出是“01”而不是“1”。它是否正确??

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

如果我不得不猜测,我会说我需要将变量转换为整数。我现在正在寻找如何做到这一点...... – 2014-09-04 20:11:16

+0

只是看看我最近的评论,也许就是这样(如果是CInt() – 2014-09-04 20:11:41

+0

接下来的事情是 - 在这里猜测:你已经有两个变量;刚刚找到除此之外,你必须设置第三个,比如:Dim Var3 - 然后Var3 = Var1 + Var2用于在Var3中获得结果;也许你只需先遮挡拦截+ fumlost?例如:http:/ /www.tutorialspoint.com/vbscript/vbscript_variables.htm – 2014-09-04 20:22:08

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