Reputation: 494
I'm having trouble converting some xml data to json data in the format it is expected. I'm new to all of these techniques and the resources I find online are difficult for me to decypher. Using the app I make in my plnkr, I can take the data in the data.json
file and use it to create the chart using a charts library. I have the xml in the data.xml
file - I think that it is in the correct format in order to be converted to json in the required format. How do I use the x2js library in order to convert the xml to json and implement it into my chart? I've been stuck on this for a while now, and haven't been able to figure out how to get it working.
Currently, the app takes the data.json
file and implements it into the data for the charts. What is the easiest way to take the xml and do the same?
Edit: I've updated my plnkr with my attempt at converting the xml data to json. I even tried simply printing out the final data in json form - if the xml converted to json, but nothing came up. I entered a x2js.js
file that has the functions from the library needed for the conversion I used in the service. I get the following error:
ReferenceError: data is not defined
at new <anonymous> (script.js:38)
at invoke (code.angularjs.org/1.2.2/angular.js:3641)
at Object.instantiate (code.angularjs.org/1.2.2/angular.js:3653)
at code.angularjs.org/1.2.2/angular.js:6684
at code.angularjs.org/1.2.2/angular.js:6097
at forEach (code.angularjs.org/1.2.2/angular.js:307)
at nodeLinkFn (code.angularjs.org/1.2.2/angular.js:6084)
at compositeLinkFn (code.angularjs.org/1.2.2/angular.js:5550)
at compositeLinkFn (code.angularjs.org/1.2.2/angular.js:5553)
at publicLinkFn (code.angularjs.org/1.2.2/angular.js:5458)
If anyone can help me out, its greatly appreciated.
Upvotes: 0
Views: 260
Reputation: 2598
OK, You have a couple of problems here:
The XML:
I took a small subset of your data and tried to see what the problem was:
<Header Name="TOP 5 AUDITORS BY INDUSTRY"><series>Industry_count</series><series>AuditFees</series><data><x>ERNST & YOUNG LLP</x><y>298</y><y>12567000</y></data></Header>
x2js doesnt seem to like the &
in your xml. Basically with the string ERNST & YOUNG
you're breaking whatever it is x2js is trying to do internally.
x2js.xml_str2json('<Header Name="TOP 5 AUDITORS BY INDUSTRY"><series>Industry_count</series><series>AuditFees</series><data><x>ERNST & YOUNG LLP</x><y>298</y><y>12567000</y></data></Header>')
doesn't work whereas
x2js.xml_str2json('<Header Name="TOP 5 AUDITORS BY INDUSTRY"><series>Industry_count</series><series>AuditFees</series><data><x>ERNST YOUNG LLP</x><y>298</y><y>12567000</y></data></Header>')
works and gives a JS object as output. The only thing missing from the second one is the &.
The Code:
I also removed the getData
function and stopped passing the data
variable into the service (You dont yet have data, which is why youre calling the service). Also added a success handler to set the scope variable.
Fully fixed working plnkr is here: http://plnkr.co/edit/uCLy42wnELYKq8S1DjYs?p=preview
Upvotes: 1