I have a big xml document that i’m trying to process into an array in javascript.
<DR> <C> <SWDGDRC> <CID>0</CID> <V>06/01/2012 00:00:00</V> </SWDGDRC> <SWDGDRC> <CID>1</CID> <V>1131</V> </SWDGDRC> <SWDGDRC> <CID>2</CID> <V>28800</V> </SWDGDRC> </C> <rowid>0</rowid> </DR> <DR> <C> <SWDGDRC> <CID>0</CID> <V>06/02/2012 00:00:00</V> </SWDGDRC> <SWDGDRC> <CID>1</CID> <V /> </SWDGDRC> <SWDGDRC> <CID>2</CID> <V /> </SWDGDRC> </C> <rowid>1000</rowid>
It consists of multiple DR(datarow) and each DR has multiple C (columns) and a rowid.
I’m trying to loop all this data into an associative array:
for(var i=0; i < DR.length; i++) { // loop DR
for(var j=0; j < DR[i].getElementsByTagName('C').length; j++) { // loop C
for(var k=0; k < DR[i].getElementsByTagName('C')[j].getElementsByTagName('SWDGDRC').length; k++) { //loop SWDGDRC
columnData[i] = {
"rowid": DR[i].getElementsByTagName('rowid')[0].textContent,
"column": {
columnID: DR[i].getElementsByTagName('C')[j].getElementsByTagName('SWDGDRC')[k].getElementsByTagName('CID')[0].textContent,
value:DR[i].getElementsByTagName('C')[j].getElementsByTagName('SWDGDRC')[k].getElementsByTagName('V')[0].textContent
}
};
}
}
}
The problem is I want to make an new array in the “column” key which loops all the C (columns) data so I can access this data using something like this: columnData[0][‘column’][0][‘columnID’]
Fixed it by using the previous code. First I create a new array with the length of the array that i’m going to loop then I set the ‘rowid’ field. This way I can access my data by: columnData[0][0][‘columnID’]