I have a node.js app that fetches some xml from a remote URL to use internally. But javascript is a more natural fit with json data, so what I have been doing is fetching the xml via an intermediate server, by calling a php script on that server, which itself fetches the xml and converts it to json(p) before returning it to my node.js app.
But I'd rather avoid the need for the intermediate server, to reduce the risk of the whole thing breaking when e.g. the intermediate server goes down. I'd rather fetch the xml direct into my node.js app, and convert it to json locally.
The relevant lines in the php script are:
$xml = simplexml_load_file($url);
$json = json_encode($xml);
This works flawlessly, creating perfect json from the xml every time. Nice and simple.
I've tried various 'xml2json' functions out there, but none of them works, or they seem to require a browser environment.
Surely there is a recognised and trusted javascript function out there that does a good job of converting xml to json? It must surely be a pretty common thing to do.
Actually, what would be ideal is a solution that works in node.js and within html, because I have two versions of the app -- one in node.js and one in html, and would rather share the same code than have different solutions for each.
Thanks!