当名称和值位于单独的 XML 节点中时,使用 Javascript 从 XML 中的某些节点解析属性? [英] Parsing properties from certain nodes in XML using Javascript when the name and the value are in separate XML nodes?

查看:20
本文介绍了当名称和值位于单独的 XML 节点中时,使用 Javascript 从 XML 中的某些节点解析属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to be able to parse some values in browser-side (no Node etc.) Javascript from an XML of the following format:

<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} 中.

Specifically, I want to be able to parse the values of CFBundleIdentifier and CFBundleShortVersionString. Suppose, I want to store them in an object metadata = {id, version}.

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

I learned from Parsing properties from certain nodes in XML using Javascript? how to parse data from XML nodes. The problem now is that unlike the abovementioned question, this format of XML doesn't store the values between the tags like so <CFBundleIdentifier>com.app.autoupdate2</CFBundleIdentifier>, but, instead, the name of the property I want to parse is between <key> tags and the property value follows after that--between the <string> tags.

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

If it can be done using XPATH, how would I form the query in this situation?

推荐答案

尝试以下方法:

#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"

然后您可以将这些分配给您想要的任何变量.

You can then assign these to whatever variables you want.

这篇关于当名称和值位于单独的 XML 节点中时,使用 Javascript 从 XML 中的某些节点解析属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆