I tried to populate a two dimensoinal array through parsing xml, but my function doesn't store the first items for some unknown reason. (So it stores [0][1] and [1][1] properly, but it does not store [0][0] and [0][1]);
The idea behind the array structure is:
first word- > first choice ->[0][0];
first word -> second choice ->[0][1];
second word -> first choice ->[1][0];
... you can guess
It alerts everytime (just to check that counters are correct.)
The XML:
<?xml version="1.0" encoding="utf-8" ?>
<Page>
<Word id = "0">
<Choice id = "0">
<text>First word - 1. choice</text>
</Choice>
<Choice id = "1">
<text>First word - 2. choice</text>
</Choice>
</Word>
<Word id= "1">
<Choices>
<Choice id = "0">
<text>Second word - First choice</text>
</Choice>
<Choice id= "1">
<text>Second word - Second Choice</text>
</Choice>
</Choices>
</Word>
</Page>
The function:
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "xml.xml",
dataType: "xml",
success: parseXml2
});
});
function parseXml2(xml) {
var myArray = [];
var a = 0;
$(xml).find("Word").each(function() {
var i = $(this).attr("id");
a = 0;
$(this).find("Choice").each(function() {
alert('I:' + i + 'A:' + a);
alert('Id:' + $(this).attr("id") + $(this).text());
myArray[i] = [];
var text = $(this).text();
myArray[i][a] = text;
a++;
});
});
alert(myArray[0][0]);
}
parseXml2(xml);
The code can also be found here.