Any recommendation for xml to json for Node.js?

前端 未结 8 695
面向向阳花
面向向阳花 2020-12-08 15:14

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 a

相关标签:
8条回答
  • 2020-12-08 15:25

    To convert XML to JSON in Node js, you can use xml2json package.

    To install the package:- npm install --save xml2json

    Add Code snippet:-

    var parser = require('xml2json');
    
    var xml = "<foo attr=\"value\">bar</foo>";
    console.log("input -> %s", xml)
    
    // xml to json
    var json = parser.toJson(xml);
    console.log("to json -> %s", json);
    

    For more details please visit:- xml2json

    0 讨论(0)
  • 2020-12-08 15:28

    sblom pointed out JsonML which might also be worth taking into consideration. Not sure about JsonML support in nodejs but there is already a jQuery plugin here.

    0 讨论(0)
  • 2020-12-08 15:30

    Chekout http://hemanth.github.com/node-rsj/

    0 讨论(0)
  • 2020-12-08 15:31

    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!

    0 讨论(0)
  • 2020-12-08 15:31

    As the other poster points out, node-xmltojs is probably the best way to go.

    If you did want to use JSONML, I'm not sure why the JQuery plugin in the other answer is being upvoted: there's JSONML for node:

    npm install jsonml
    

    Example:

    var fs = require('fs'),
      parse = require('jsonml').parse;
    
    var jsonML = parse(fs.readFileSync('myfile.xml'));
    
    0 讨论(0)
  • 2020-12-08 15:37

    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.

    0 讨论(0)
提交回复
热议问题