xml用vbscript解析

问题描述:

你好,我想从xml解析一个特定的信息。在那个XML代码中,我想读取美元的ForexBuying。我得到MSGBOX时,它的美元,但我想看到它ForexBuyingxml用vbscript解析

我很新在VBScript请帮我

Sub GetExchangeRate() 
    Dim xmlDoc, Currencyy, plot, ForexBuying 

    Set xmlDoc = CreateObject("Msxml2.DOMDocument") 
    xmlDoc.load("......\exchange.xml") 

    Set Currencyy = xmlDoc.getElementsByTagName("CurrencyName") 

    If Currencyy.length > 0 Then 
     For Each x In Currencyy 
      If x.text="US DOLLAR" Then 
       MsgBox(x.Text) 
      End If 
     Next 
    Else 
     MsgBox("Parse Error") 
    End If 
End Sub 

Call GetExchangeRate() 

XML:

<Tarih_Date Tarih="01.10.2014" Date="10/01/2014" Bulten_No="2014/189"> 
    <Currency CrossOrder="0" Kod="USD" CurrencyCode="USD"> 
     <Unit>1</Unit> 
     <Isim>ABD DOLARI</Isim> 
     <CurrencyName>US DOLLAR</CurrencyName> 
     <ForexBuying>2.2829</ForexBuying> 
     <ForexSelling>2.2871</ForexSelling> 
     <BanknoteBuying>2.2813</BanknoteBuying> 
     <BanknoteSelling>2.2905</BanknoteSelling> 
     <CrossRateUSD>1</CrossRateUSD> 
     <CrossRateOther></CrossRateOther> 
    </Currency> 
    <Currency CrossOrder="1" Kod="AUD" CurrencyCode="AUD"> 
     <Unit>1</Unit> 
     <Isim>AVUSTRALYA DOLARI</Isim> 
     <CurrencyName>AUSTRALIAN DOLLAR</CurrencyName> 
     <ForexBuying>1.9833</ForexBuying> 
     <ForexSelling>1.9962</ForexSelling> 
     <BanknoteBuying>1.9742</BanknoteBuying> 
     <BanknoteSelling>2.0082</BanknoteSelling> 
     <CrossRateUSD>1.1484</CrossRateUSD> 
     <CrossRateOther></CrossRateOther> 
    </Currency> 
</Tarih_Date> 
+1

一般暗示,没有'Optio时使用'Dim'没有任何好处n在脚本的第一行显示。你*绝对应该*在脚本的第一行中。 – Tomalak 2014-10-01 17:27:18

再好的是Ekkehard.Horner的答案,这里是你的风格的解决:

option explicit 
On Error GoTo 0 
Dim strResult: strResult = Wscript.ScriptName 
Sub GetExchangeRate() 
    dim xmlDoc, Currencyy, plot, FxBuying, xx, fx 
Set xmlDoc = CreateObject("Msxml2.DOMDocument") 
xmlDoc.load("....\exchange.xml") 
Set Currencyy = xmlDoc.getElementsByTagName("CurrencyName") 
If Currencyy.length > 0 then 
    For each xx in Currencyy 
     if xx.text="US DOLLAR" then 
      strResult = strResult & vbNewLine & xx.TagName & "=" & xx.Text 
      Set FxBuying = xx.parentNode.getElementsByTagName("ForexBuying") 
      If FxBuying.length > 0 then 
      For each fx in FxBuying 
       strResult = strResult & vbTab & fx.TagName & "=" & fx.Text 
      Next 
      Else 
      strResult = strResult & vbTab & "no value" 
      End If 
     end if 
    Next 
else 
    strResult = strResult & vbNewLine & "Parse Error" 
End If 
End Sub 
Call GetExchangeRate() 
Wscript.Echo strResult 
Wscript.Quit 

输出:

cscript.exe //NoLogo "W3138975.vbs" 
W3138975.vbs 
CurrencyName=US DOLLAR ForexBuying=2.2829 

只需使用XPath(正常,见here)。如:

Dim oFS  : Set oFS  = CreateObject("Scripting.FileSystemObject") 
Dim sFSpec : sFSpec  = oFS.GetAbsolutePathName("..\testdata\xml\26144567.xml") 
Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument") 
objMSXML.setProperty "SelectionLanguage", "XPath" 
objMSXML.async = False 
objMSXML.load sFSpec 

If 0 = objMSXML.parseError Then 
    Dim sXPath : sXPath  = "/Tarih_Date/Currency[CurrencyName = ""US DOLLAR""]/ForexBuying" 
    Dim ndDollar : Set ndDollar = objMSXML.selectSingleNode(sXPath) 
    If ndDollar Is Nothing Then 
     WScript.Echo sXPath, "failed" 
    Else 
     WScript.Echo "ForexBuying:", ndDollar.text 
    End If 
Else 
    WScript.Echo objMSXML.parseError.reason 
End If 

输出:

cscript 26144567.vbs 
ForexBuying: 2.2829