2015-04-22 54 views
1

我有這個XML文件,需要能夠將PackageID的值和文本的值提取到Application.IDs的字符串中,該名稱在'selectedApplications'下面列出的xml代碼。VB腳本XML查詢

EG在這個XML文件中Application.ID的'selectedApplications'= 1和5下,所以我希望能夠像這樣返回下面的Argon和AusKey應用程序的變量(它需要忽略帶ID的應用程序10在這個例子中,生成的XML文件中的應用將永遠是不同的
任何想法將是巨大的三江源

STR1等於MEL0089F:!SilentInstall

STR2等於MEL007F0:安裝

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> 
+0

嗨我已經能夠返回包ID和文本與下面的代碼,但我必須硬編碼在應用程序名稱不會做,因爲應用程序名稱將永遠改變,因此我需要它返回字符串基於ID的是在 'SelectedApplications' –

+0

設置xmlDoc中= _ 的CreateObject( 「Microsoft.XMLDOM」) xmlDoc.Async = 「假」 xmlDoc.Load( 「AppDiscoveryresult.xml」) 設置colNodes = xmlDoc.selectNodes _ (「// ApplicationGroup/Application/Program [@ PackageName ='Argon']」) 對於colNodes中的每個objNode Wscript.Echo objNode.Text Wscript.Echo objNode.Attributes.getNamedItem(「PackageId」)。Text Wscript.Echo Next –

回答

0

有關XPath示例,請參見here

請參閱here(並遵循鏈接)爲XML腳本提供可重複使用的防禦骨架/模板。無論是在代碼

合併:

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 

輸出:

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

如何應對AMD64 VS 86留作練習。

+0

非常感謝Thankyou!那很棒。 –