Hello i want to parse a specific info from xml. In that xml code i want to read ForexBuying of the US DOLLAR. I get msgbox when it\'s US DOLLAR but i want to see its ForexBuyin
No matter how good is Ekkehard.Horner's answer, here is your-style solving:
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
Output:
cscript.exe //NoLogo "W3138975.vbs"
W3138975.vbs
CurrencyName=US DOLLAR ForexBuying=2.2829
Just use XPath (properly, see here). As in:
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
output:
cscript 26144567.vbs
ForexBuying: 2.2829