当名称和值在单独的XML节点中时,使用Javascript从XML中的某些节点解析属性?



我希望能够从以下格式的XML中解析浏览器端(无Node等)Javascript中的一些值:

<list>
<dict>
<key>BuildMachineOSBuild</key>
<string>16B2555</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>App AutoUpdate</string>
<key>CFBundleExecutable</key>
<string>App AutoUpdate</string>
<key>CFBundleIconFile</key>
<string>AppIcon</string>
<key>CFBundleIdentifier</key>
<string>com.app.autoupdate2</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>App AutoUpdate</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>3.8.1</string>
<key>CFBundleSignature</key>
<string>MSau</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>CFBundleVersion</key>
<string>3.8.16112200</string>
<key>DTCompiler</key>
<string>com.apple.compilers.llvm.clang.1_0</string>
<key>DTPlatformBuild</key>
<string>8B62</string>
<key>DTPlatformVersion</key>
<string>GM</string>
<key>DTSDKBuild</key>
<string>16B2649</string>
<key>DTSDKName</key>
<string>macosx10.12</string>
<key>DTXcode</key>
<string>0810</string>
<key>DTXcodeBuild</key>
<string>8B62</string>
<key>LSHasLocalizedDisplayName</key>
<true/>
<key>LSMinimumSystemVersion</key>
<string>10.10</string>
<key>LSRequiresNativeExecution</key>
<true/>
<key>MbuLoggingFullLegacyUpload</key>
<true/>
<key>AppBuildNumber</key>
<string>161122</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSMainNibFile</key>
<string>SAUMainWindow</string>
<key>NSPrincipalClass</key>
<string>SAUApplication</string>
</dict>
</list>

具体来说,我希望能够解析CFBundleIdentifierCFBundleShortVersionString的值。假设,我想将它们存储在对象metadata = {id, version}中。

我从解析属性从某些节点在XML使用Javascript?如何解析来自XML节点的数据。现在的问题是,与上述问题不同,这种XML格式不像<CFBundleIdentifier>com.app.autoupdate2</CFBundleIdentifier>那样在标记之间存储值,而是,相反,我想要解析的属性的名称在<key>标记之间,属性值随后在<string>标记之间。

如果可以使用XPATH完成,那么在这种情况下如何形成查询呢?

试着这样做:

#this assumes your xml string is assigned to xmldoc
let domElement = new DOMParser().parseFromString(xmldoc,'text/xml');
identifiers = ["CFBundleIdentifier","CFBundleShortVersionString"]
for (let id of identifiers){
exp = `//key[text()="${id}"]/following-sibling::string[1]/text()`;
let target = domElement.evaluate(exp, domElement, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
console.log(target.snapshotItem(0).nodeValue);    
}

输出:

"com.app.autoupdate2"
"3.8.1"

你可以把它们赋值给任何你想要的变量。

使用JavaScript和浏览器的XPath 1.0evaluate方法:

const xml = `<list>
<dict>
<key>BuildMachineOSBuild</key>
<string>16B2555</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>App AutoUpdate</string>
<key>CFBundleExecutable</key>
<string>App AutoUpdate</string>
<key>CFBundleIconFile</key>
<string>AppIcon</string>
<key>CFBundleIdentifier</key>
<string>com.app.autoupdate2</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>App AutoUpdate</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>3.8.1</string>
<key>CFBundleSignature</key>
<string>MSau</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>CFBundleVersion</key>
<string>3.8.16112200</string>
<key>DTCompiler</key>
<string>com.apple.compilers.llvm.clang.1_0</string>
<key>DTPlatformBuild</key>
<string>8B62</string>
<key>DTPlatformVersion</key>
<string>GM</string>
<key>DTSDKBuild</key>
<string>16B2649</string>
<key>DTSDKName</key>
<string>macosx10.12</string>
<key>DTXcode</key>
<string>0810</string>
<key>DTXcodeBuild</key>
<string>8B62</string>
<key>LSHasLocalizedDisplayName</key>
<true/>
<key>LSMinimumSystemVersion</key>
<string>10.10</string>
<key>LSRequiresNativeExecution</key>
<true/>
<key>MbuLoggingFullLegacyUpload</key>
<true/>
<key>AppBuildNumber</key>
<string>161122</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSMainNibFile</key>
<string>SAUMainWindow</string>
<key>NSPrincipalClass</key>
<string>SAUApplication</string>
</dict>
</list>`;
const xmlDoc = new DOMParser().parseFromString(xml, 'application/xml');
console.log(["CFBundleIdentifier","CFBundleShortVersionString"].map(key => { return { [key] : xmlDoc.evaluate(`string(//key[. = '${key}']/following-sibling::*[1])`, xmlDoc, null, XPathResult.STRING_TYPE, null).stringValue };}));

使用JavaScript和Saxon-JS 2开发XPath 3.1:

const xml = `<list>
<dict>
<key>BuildMachineOSBuild</key>
<string>16B2555</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>App AutoUpdate</string>
<key>CFBundleExecutable</key>
<string>App AutoUpdate</string>
<key>CFBundleIconFile</key>
<string>AppIcon</string>
<key>CFBundleIdentifier</key>
<string>com.app.autoupdate2</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>App AutoUpdate</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>3.8.1</string>
<key>CFBundleSignature</key>
<string>MSau</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>CFBundleVersion</key>
<string>3.8.16112200</string>
<key>DTCompiler</key>
<string>com.apple.compilers.llvm.clang.1_0</string>
<key>DTPlatformBuild</key>
<string>8B62</string>
<key>DTPlatformVersion</key>
<string>GM</string>
<key>DTSDKBuild</key>
<string>16B2649</string>
<key>DTSDKName</key>
<string>macosx10.12</string>
<key>DTXcode</key>
<string>0810</string>
<key>DTXcodeBuild</key>
<string>8B62</string>
<key>LSHasLocalizedDisplayName</key>
<true/>
<key>LSMinimumSystemVersion</key>
<string>10.10</string>
<key>LSRequiresNativeExecution</key>
<true/>
<key>MbuLoggingFullLegacyUpload</key>
<true/>
<key>AppBuildNumber</key>
<string>161122</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSMainNibFile</key>
<string>SAUMainWindow</string>
<key>NSPrincipalClass</key>
<string>SAUApplication</string>
</dict>
</list>`;

console.log(["CFBundleIdentifier","CFBundleShortVersionString"].map(key => SaxonJS.XPath.evaluate(`parse-xml($xml)//key[. = $key] ! map { $key : following-sibling::*[1]/string() }`, [], { params : { xml : xml, key : key } })));
<script src="https://www.saxonica.com/saxon-js/documentation/SaxonJS/SaxonJS2.rt.js"></script>

最新更新