Jump to content

Populating Javascript multidimensional array from xml


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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...