1

I have an XML which contain set of 3 Host details.

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.

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>

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

1 Answer 1

1

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

output:

ipAddress: 172.16.120.29
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.