1

I have this XML file and need to be able to extract the value of the PackageID and the value of the text to a string for the Application.IDs that are listed under 'selectedApplications' which is at the end of the xml code.

E.G in this XML file the Application.ID's under 'selectedApplications' = 1 and 5 so I would like to be able to return the below variables for the Argon and AusKey applications like this (it needs to ignore the application with ID 10 in this example the applications in the generated XML file will always be different.
Any Ideas would be great Thankyou!

str1 equals MEL0089F:SilentInstall

str2 equals MEL007F0:Install

enter code here

<?xml version="1.0" encoding="utf-8"?>
<Applications RootDisplayName="Applications">
<ApplicationGroup Name="A -E">

<Application DisplayName="Argon" State="enabled" Id="1">
        <Setter Property="description"/>
        <Program Architecture="amd64" PackageId="MEL0089F" PackageName="Argon">SilentInstall</Program>
        <Program Architecture="x86" PackageId="MEL0089F" PackageName="Argon">SilentInstall</Program>
        <Dependencies/>
        <Filters/>
        <ApplicationMappings>
            <Match Type="MSI" OperatorCondition="OR" DisplayName="Argon">
                <Setter Property="ProductId">{AF7D1510-2FFB-49DF-84E6-03F5B1626B60}</Setter>
            </Match>
        </ApplicationMappings>
    </Application>

    <Application DisplayName="AUSKey" State="enabled" Id="5">
        <Setter Property="description"/>
        <Program Architecture="amd64" PackageId="MEL007F0" PackageName="AUSKey">Install</Program>
        <Program Architecture="x86" PackageId="MEL007F0" PackageName="AUSKey">Install</Program>
        <Dependencies/>
        <Filters/>
        <ApplicationMappings>
            <Match Type="MSI" OperatorCondition="OR" DisplayName="AUSkey software 1.4.4">
                <Setter Property="ProductId">{24D37B30-83B4-46A7-A691-30F2FCEAE58E}</Setter>
            </Match>
        </ApplicationMappings>
    </Application>

    <Application DisplayName="AutoIT" State="enabled" Id="10">
        <Setter Property="description"/>
        <Program Architecture="amd64" PackageId="MEL0078A" PackageName="AutoIT">SilentInstall</Program>
        <Program Architecture="x86" PackageId="MEL0078A" PackageName="AutoIT">SilentInstall</Program>
        <Dependencies/>
        <Filters/>
        <ApplicationMappings/>
    </Application>

</ApplicationGroup>

<SelectedApplications><SelectApplication Application.Id="1"/><SelectApplication Application.Id="5"/></SelectedApplications></Applications>
  • Hi I've been able to return the Package ID and text with the below code but I have to hardcode in the application name which wont do as the applications names will always change thus I need it to return the strings based on the ID's that are in 'SelectedApplications' – Peter Williams Apr 22 '15 at 06:56
  • Set xmlDoc = _ CreateObject("Microsoft.XMLDOM") xmlDoc.Async = "False" xmlDoc.Load("AppDiscoveryresult.xml") Set colNodes=xmlDoc.selectNodes _ ("//ApplicationGroup/Application/Program[@PackageName='Argon']") For Each objNode in colNodes Wscript.Echo objNode.Text Wscript.Echo objNode.Attributes.getNamedItem("PackageId").Text Wscript.Echo Next – Peter Williams Apr 22 '15 at 06:57

1 Answers1

0

See here for XPath examples.

See here (and follow the links) for a re-usable defensive skeleton/template for XML scripts.

Merge both in code:

Option Explicit

Dim xmlObj : Set xmlObj = CreateObject("msxml2.domdocument")
xmlObj.async = False
xmlObj.Load "..\data\29789813.xml"
If xmlObj.parseError.errorCode <> 0 Then
    WScript.Echo "Error Reading File - " & xmlObj.parseError.reason
Else
    Dim sXPath : sXPath     = "/Applications/SelectedApplications/SelectApplication"
    Dim ndlSA  : Set ndlSA  = xmlObj.selectNodes(sXPath)
    If 0 = ndlSA.length Then
       WScript.Echo "failed:", sXPath
    Else
       Dim ndSA
       For Each ndSA in ndlSA
           Dim sId : sId = ndSA.getAttribute("Application.Id")
           sXPath = "/Applications/ApplicationGroup/Application[@Id='" & sId & "']/Program[@Architecture='amd64']"
           Dim ndA : Set ndA = xmlObj.selectSingleNode(sXPath)
           If ndA Is Nothing Then
              WScript.Echo "failed:", sXPath
           Else
              WScript.Echo "found:", ndSA.tagName, sId, ndA.tagName, ndA.text
           End If
       Next
    End If
End If

output:

cscript 29789813.vbs
found: SelectApplication 1 Program SilentInstall
found: SelectApplication 5 Program Install

How to deal with amd64 vs x86 is left as an exercise.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96