Any recommendation for xml to json for Node.js?


Question

I've installed node-xml but I don't think it works the way I expect. and it doesnt' have example. any recommendation for xml-2-json (js) for node.js? I also looked at xml2js in npm but it is deprecated and people reported that it is broken with the latest node.js

by the way, i'm also using express. :)

1
23
4/15/2011 3:40:38 AM

Accepted Answer

There are many xml parsers.

Like libxmljs and node-o3-xml. The latter is made and used by Ajax.org so it should be stable.

As for converting XML to JSON, I would recommend creating an object structure from your xml and then manually calling JSON.stringify on it. This gives you complete control of how your xml data is turned into JSON.

You can then either save the JSON in a file/DB or serve it to a request.

16
2/27/2018 5:51:30 PM

libxmljs and node-o3-xml are great and fast, but beware that they both need to compile binary components. And if you are using them for a module that will be used by others, that caveat is even more serious.

Taking a higher-level view for a moment, remember that node is single-threaded. So, any XML parsing you do is going to block the node process. To me, that means that XML parsing should never be performed in the same process as your main app. Then, once you move XML parsing to a separate process, maybe a little speed can be sacrificed in favor of ease of installation and greater portability.

Personally, that's why I use sax.js -- a pure JavaScript SAX parser -- in my feedparser library (if you're parsing RSS/Atom/RDF feeds, please consider trying it -- comments and pull requests are more than welcome). And honestly, when parsing something as big as an RSS feed, there is no discernable speed difference between sax.js and libxmljs. If you're parsing enormous XML files, you may notice a difference, I suppose. But even then, one nice thing about sax.js is the streaming. Unlike libxmljs (last I used it), you can pipe a stream into sax.js rather than having to read the entire XML document into memory. If you're parsing enormous files, you will love that!


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon