0

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!

drmrbrewer
  • 11,491
  • 21
  • 85
  • 181
  • The accepted answer here looks like a good solution: http://stackoverflow.com/questions/1773550/convert-xml-to-json-and-back-using-javascript – kakoma Feb 08 '15 at 08:31
  • 1
    PHP's way happens to work not really because of `json_encode`, but because of the magic that is the SimpleXML object. `json_encode` is merely able to encode objects, and SimpleXML happens to magically expose everything as object properties. The question you really need to be asking is how to convert an XML file into a Javascript object structure which can then be easily encoded using standard JSON encoding functions; I'd suggest you update your question title accordingly. – deceze Feb 08 '15 at 08:32
  • Related to something that you wanted http://stackoverflow.com/questions/5672151/any-recommendation-for-xml-to-json-for-node-js May be this will work for you – vamshi mutyala Feb 08 '15 at 12:02
  • Bit confused as to why this post has been put on hold as "off-topic". Doesn't seem off topic to me! – drmrbrewer Feb 09 '15 at 08:06

1 Answers1

1

Since you're already running node, check npm. I have used with ee-xml-to-json with no problems; although, I haven't tried using it client-side. You may be able to use it with browserify if really want to be able to do this client-side.

Good luck :)

  • Since I was looking for a standalone solution that wasn't tied to node.js (I want to use it in my html too), I hadn't really considered a node.js package. But your answer made me think again, and it's what I'm doing for now. As you say, I'm already running node, so within node I can call the xml2json function direct, and within html I can just to an XMLHttpRequest to my nodeserver. Although that is still an intermediary, it's one that I need to be running anyway. I'm using https://www.npmjs.com/package/xml2json and it seems pretty good. Thanks. – drmrbrewer Feb 09 '15 at 08:05