使用VBScript解析XML文件

时间:2014-05-22 14:19:13

标签: xml parsing vbscript

我有一个包含3个主机详细信息的XML。

我想提取“ipAddress”的值,仅适用于“AppService”类型为“YesThisIWant”的主机,并将其打印到控制台或其他任何内容。

我对“AppService”类型为“无用”的主机的“ipAddress”不感兴趣。

下面是我试过的代码,但我不知道如何设置检查条件。以下代码根本不是主机的打印类型:( 请帮帮我。

         <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:

    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

1 个答案:

答案 0 :(得分:0)

使用具有所需类型的App Service查询,“返回”主机,提取IP地址。在代码中:

  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