简体   繁体   中英

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

Here is an example of the plist that was provided to me:

<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. 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 :

 // 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.

The object xmlDoc contains a DOM with the parsed XML and you can navigate it to get evert node you need. In the example above a loop over the key/value objects inside <dict> element and print them. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM