[英]VB Script XML Query
我有這個XML文件,需要能夠將PackageID的值和文本的值提取到Application.ID的字符串中,該字符串位於xml代碼結尾的“ selectedApplications”下。
例如,在此XML文件中,例如,“ selectedApplications” = 1和5下的Application.ID為EG,因此我希望能夠為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>
有關XPath示例,請參見此處 。
有關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 x86。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.