简体   繁体   English

我试图在plist版本中读取.strings文件。 我有 <key> 和 <string> 。 我不确定如何在我的JavaScript代码中阅读这些输入。

[英]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.

相关问题 我在我的 node.js 代码中收到此错误“无法读取未定义的属性 'split'”。 如何解决这个问题? - I am getting this error “Cannot read property 'split' of undefined” in my node.js code. How to resolve this? 我正在尝试使用 javascript new fileReader() 读取文件 - I am trying to read the file using javascript new fileReader() 我正在尝试将Material-UI InfoIcon添加到我的代码中。 但我不知道如何在TextField中实现它 - I am trying to add the Material-UI InfoIcon into my code. But I don't know how to implement it in TextField 我正在尝试通过php代码更新记录。 但是我的代码有麻烦 - I am trying to update records by php code. but having trouble in my code 当我试图确保我的 javascript 代码正常工作时,我在 Google Chrome 上的控制台上收到此错误 - I get this error on my console on Google Chrome when I am trying to make sure that my javascript code is working 我试图将CORS正确地实现到我的JavaScript代码,并且工作了一段时间,我相信项目中缺少一些文件吗? - I am trying to implement CORS to my JavaScript code correctly, and it worked for a while, I believe I am missing some file from the project? 不是重大错误,但我收到“TypeError:无法读取未定义的属性”。 我不确定我的下一步行动是什么 - Not a major error but I am getting `TypeError: Cannot read property 'has' of undefined`. I am not sure what my next move will be 我正在尝试使用HTML创建GUI,但是我的CSS代码未加载。 我不确定错误是什么 - I am trying to make a GUI Using HTML, but my CSS code is not loading. I am not sure what the error is 尝试编译代码时遇到错误。 我哪里出错了,我该如何解决? - I'm facing an error while trying to compile the code. Where am I going wrong and How do I fix it? 我正在尝试优化第一块代码。 为什么我的方法不起作用? 缩短代码的最佳方法是什么? - I am trying to optimize the first chunk of code. Why doesn't my approach work? What's the best approach to shorten the code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM