Reputation: 69
Suppose to have an xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<Langs>
<dirs>
<string>uk-fr</string>
<string>uk-it</string>
<string>uk-pl</string>
<string>uk-tr</string>
</dirs>
<langs>
<Item key="af" value="Afrikaans" />
<Item key="ar" value="Arabic" />
<Item key="az" value="Azerbaijani" />
<Item key="ba" value="Bashkir" />
<Item key="be" value="Belarusian" />
</langs>
</Langs>
How can I get an array that contains the key of each item? Using javascipt? An object as this:
var array = [af,ar,az,ba,be]
Upvotes: 3
Views: 6403
Reputation: 2666
Sorry for the late reply.
Here's an example code on how to use jQuery.parseXML
to solve the problem:
var xml_string = 'your xml string';
var output = $.parseXML(xml_string); // <-------- parsing using jQuery.parseXML
var array = []; // <----------------------------- array for storing the keys
// iterating over the elements where selector 'langs>Item' matches in the parsed XML
// and retrieving the key values and pushing them into the array
$('langs>Item', output).each(function(i, e) {
array.push($(e).attr('key'));
});
// display the output
console.log(array);
Here's a fiddle.
Upvotes: 1