Jump to content

Populating Javascript multidimensional array from xml


Soggy
 Share

Recommended Posts

Hi,

 

I am fairly new to javascript and still finding my feet so if i am not been clear in my question let me know.

 

I am using the chart.js to build graphs in my docuemnts and it works great, I am now trying to expand this by linking to an xml file I have set up.

 

If I use the following code I can retrieve the data fine:

 

 

xmlDoc=loadXMLDoc("rego.xml");var nSubOrg = xmlDoc.getElementsByTagName('SubOrg');var nRev = xmlDoc.getElementsByTagName('Revenue');var nCol = xmlDoc.getElementsByTagName('Colour');var nHL = xmlDoc.getElementsByTagName('HLight');    //chart data for Chart.JS            var dData = [                {                    value: (nRev[0].childNodes[0].nodeValue),                    color: (nCol[0].childNodes[0].nodeValue),                    highlight: (nHL[0].childNodes[0].nodeValue),                    label: (nSubOrg[0].childNodes[0].nodeValue)                },                {                    value: (nRev[1].childNodes[0].nodeValue),                    color: (nCol[1].childNodes[0].nodeValue),                    highlight: (nHL[1].childNodes[0].nodeValue),                    label: (nSubOrg[1].childNodes[0].nodeValue)                },                {                    value: (nRev[2].childNodes[0].nodeValue),                    color: (nCol[2].childNodes[0].nodeValue),                    highlight: (nHL[2].childNodes[0].nodeValue),                    label: (nSubOrg[2].childNodes[0].nodeValue)                }];

 

However I am trying to make this a bit more functional so want to loop through to create the multidimension array. I have found other snippets which gve me an idea about how to do this but I just can't get it work.

 

I think something like this should but can't get my head round the exact syntax:

 

 

xmlDoc=loadXMLDoc("rego.xml");var nSubOrg = xmlDoc.getElementsByTagName('SubOrg');var nRev = xmlDoc.getElementsByTagName('Revenue');var nCol = xmlDoc.getElementsByTagName('Colour');var nHL = xmlDoc.getElementsByTagName('HLight');//try and build arrayvar dData=new array[()]for (i=0; i<nRev.length; i++)    {      dData[i].push(                    value: (nRev[i].childNodes[0].nodeValue),                    color: (nCol[i].childNodes[0].nodeValue),                    highlight: (nHL[i].childNodes[0].nodeValue),                    label: (nSubOrg[i].childNodes[0].nodeValue)                );    };

 

Any help would be appreciated.

 

Cheers

 

Soggy

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...