[英]I am trying to read a .strings file in plist version. I have <key> and <string>. I am not sure how to read these inputs in my javascript code.
I already have a javascript code where I type a string and find what is expected in my predictive cell and tap on it. 我已经有了一个javascript代码,我输入一个字符串,找到预测单元格中的预期并点击它。 Very simple and working fine.
非常简单,工作正常。 However now I have given a plist data which I am not sure how to read it and use it in my code.
但是现在我给出了一个plist数据,我不知道如何阅读它并在我的代码中使用它。
Here is an example of the plist that was provided to me: 以下是提供给我的plist示例:
<plist version="1.0">
<dict>
<key>Animal</key>
<string>cat | dog | chicken | cow</string>
<key>Fruits</key>
<string>apple | cherries | kiwi</string>
</dict>
</plist>
So first how do I read each section? 首先,我如何阅读每个部分? I understand that when you type cat, expected result would be animal according to this plist.
我明白,当你输入猫时,根据这个plist,预期结果将是动物。 So my question is how to do following:
所以我的问题是如何做到以下几点:
I want to read the and assign it to a value to be used later I also need to read and assign in to an array 我想读取并将其分配给稍后要使用的值我还需要读取并分配给数组
example: 例:
var inputStringArray = ["cat","dog","chicken","cow"];
var expectedInput = "Animal";
the "plist" format is an XML so you can parse a plist as any other XML in JavaScript using a DOMParser
: “plist”格式是一个XML,因此您可以使用
DOMParser
将plist解析为JavaScript中的任何其他XML:
// Code goes here var plistString = '<plist version="1.0"><dict><key>Animal</key><string>cat | dog | chicken | cow</string><key>Fruits</key><string>apple | cherries | kiwi</string></dict></plist>'; var parser = new DOMParser() var xmlDoc = parser.parseFromString(plistString, "text/xml"); var dictEl = xmlDoc.getElementsByTagName('dict')[0]; var keyAndValues = dictEl.childNodes; for(var i = 0; i < keyAndValues.length; i++) { var el = keyAndValues[i]; console.log(el.nodeName, '-', el.textContent); }
You create a DOMParser
and parse your plist with parseFromString()
method. 您创建一个
DOMParser
并使用parseFromString()
方法解析您的plist。
The object xmlDoc
contains a DOM with the parsed XML and you can navigate it to get evert node you need. 对象
xmlDoc
包含带有解析XML的DOM,您可以导航它以获得所需的外部节点。 In the example above a loop over the key/value objects inside <dict>
element and print them. 在上面的示例中,循环遍历
<dict>
元素内的键/值对象并打印它们。 Running the code above, you should see in console the following lines: 运行上面的代码,您应该在控制台中看到以下行:
key - Animal
string - cat | dog | chicken | cow
key - Fruits
string - apple | cherries | kiwi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.