使用 VBScript 解析 XML 文件 [英] XML File parsing using the VBScript
问题描述
我有一个包含 3 个主机详细信息的 XML.
I have an XML which contain set of 3 Host details.
我想提取ipAddress"的值,只针对AppService"类型为YesThisIWant"的主机,并打印到控制台或任何东西.
I want to extract the value of "ipAddress", only for the host whose "AppService" Type is "YesThisIWant" and print it to the console or anything.
我对AppService"类型为Useless"的主机的ipAddress"不感兴趣.
I am not intrested in the "ipAddress" of the host whose "AppService" Type is "Useless".
下面是我试过的代码,但我不知道如何设置检查条件.此外,以下代码根本不是主机的打印类型:(请帮帮我.
Below is the code i tried, but i dont know how to put check condition. Also the below code is not at all printing Type for the host :( Please help me out.
<CsaPhoneBook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CsaPhoneBook.xsd"><Version>V1</Version>
<Host>
<name>localhost</name>
<ipAddress>127.0.0.1</ipAddress>
<ip6Address/>
<NetServices/>
<AppServices>
<AppService Type="Useless">
<Collection>
<element>
<name isKey="1">LogicalName</name>
<value>KZWGLDPOUYSQ</value>
</element>
</Collection>
<NetServices>
<NetService>
<Action>open</Action>
<PortNr>108</PortNr>
<Protocol>TCP</Protocol>
</NetService>
</NetServices></AppService>
<AppService Type="Useless">
<Collection>
<element>
<name isKey="1">LogicalName</name>
<value>MVXCEFHKPQZS</value>
</element>
</Collection>
<NetServices><NetService><Action>open</Action><PortNr>104</PortNr><Protocol>TCP</Protocol></NetService></NetServices></AppService>
</AppServices>
<Connection Type="LAN"><LAN/></Connection>
</Host>
<Host>
<name>BLRKMIS0897PC</name>
<ipAddress>172.16.120.29</ipAddress>
<ip6Address/><NetServices/>
<AppServices>
<AppService Type="YesThisIWant">
<Collection>
<element>
<name isKey="1">LogicalName</name>
<value>BLRKMIS0897PC</value>
</element>
</Collection>
</AppService>
</AppServices><Connection Type="LAN"><LAN/></Connection>
</Host>
<Host><name>BLRKMIS1172PC</name><ipAddress>172.16.120.36</ipAddress><ip6Address></ip6Address><NetServices/><AppServices><AppService Type="Useless">
<Collection>
<element>
<name isKey="1">LogicalName</name>
<value>BLRKMIS1172PC</value>
</element>
</Collection>
<NetServices><NetService><Action>open</Action><PortNr>104</PortNr><Protocol>TCP</Protocol></NetService></NetServices></AppService>
</AppServices><Connection Type="LAN"><LAN/></Connection></Host>
</CsaPhoneBook>
这是我尝试过的 VBScript:
And here is the VBScript i tried:
VBScript:
Dim sFSpec : sFSpec = "Some.xml"
Dim sXPathName : sXPath = "/CsaPhoneBook/Host"
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
Dim oXDoc : Set oXDoc = CreateObject( "Msxml2.DOMDocument.6.0" )
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.load sFSpec
For Each Host In oXDoc.SelectNodes("//Host")
For Each AppServices In Host.SelectNodes("./AppServices")
For Each AppService In Host.SelectNodes("./AppService")
Type = AppService.getAttribute("Type")
MsgBox Type
Next
Next
Next
推荐答案
使用所需类型的 Appservice 查询,返回"到主机,提取 IP 地址.在代码中:
Use a query for the Appservice with the desired Type, 'go back' to the Host, extract the IPAddress. In code:
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec = goFS.GetAbsolutePathName("..\testdata\xml\23809494.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 = "/CsaPhoneBook/Host/AppServices/AppService[@Type=""YesThisIWant""]"
Dim ndlWantedHosts : Set ndlWantedHosts = objMSXML.selectNodes(sXPath)
If 0 = ndlWantedHosts.length Then
WScript.Echo sXPath, "failed"
Else
Dim ndWantedHost
For Each ndWantedHost In ndlWantedHosts
WScript.Echo "ipAddress:", ndWantedHost.parentNode.parentNode.selectSingleNode("ipAddress").text
Next
End If
Else
WScript.Echo objMSXML.parseError.reason
End If
输出:
ipAddress: 172.16.120.29
这篇关于使用 VBScript 解析 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!