Node.js 的 xml 到 json 有什么推荐吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5672151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:02:19  来源:igfitidea点击:

Any recommendation for xml to json for Node.js?

xmljsonnode.js

提问by murvinlai

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

我已经安装了 node-xml,但我认为它不像我期望的那样工作。它没有例子。对 node.js 的 xml-2-json (js) 有什么建议吗?我还在 npm 中查看了 xml2js,但它已被弃用,人们报告说它被最新的 node.js 破坏了

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

顺便说一句,我也在使用快递。:)

采纳答案by Raynos

There are many xml parsers.

有很多xml 解析器

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

libxmljsnode-o3-xml。后者是由 Ajax.org 制作和使用的,因此它应该是稳定的。

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

至于将 XML 转换为 JSON,我建议从您的 xml 创建一个对象结构,然后手动调用JSON.stringify它。这使您可以完全控制如何将 xml 数据转换为 JSON。

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

然后,您可以将 JSON 保存在文件/数据库中或将其提供给请求。

回答by danmactough

libxmljsand node-o3-xmlare 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.

libxmljsnode-o3-xml很棒而且速度很快,但要注意它们都需要编译二进制组件。如果您将它们用于其他人将使用的模块,则警告更加严重。

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.

稍等片刻,请记住节点是单线程的。因此,您所做的任何 XML 解析都会阻塞节点进程。对我来说,这意味着永远不应在与主应用程序相同的进程中执行 XML 解析。然后,一旦您将 XML 解析转移到一个单独的进程中,也许可以牺牲一点速度来简化安装和提高可移植性。

Personally, that's why I use sax.js-- a pure JavaScript SAX parser -- in my feedparserlibrary (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!

就个人而言,这就是我在我的feedparser库中使用sax.js—— 一个纯 JavaScript SAX 解析器的原因(如果你正在解析 RSS/Atom/RDF 提要,请考虑尝试它 —— 非常欢迎评论和拉取请求) . 老实说,在解析像 RSS 提要这样大的内容时,sax.js 和 libxmljs 之间没有明显的速度差异。如果您正在解析巨大的 XML 文件,我想您可能会注意到不同之处。但即便如此,关于 sax.js 的一件好事是流媒体。与 libxmljs(我上次使用它)不同,您可以将流通过管道传输到 sax.js,而不必将整个 XML 文档读入内存。如果您正在解析巨大的文件,您会喜欢的!

回答by Zugwalt

Where do you see that xml2jsis deprecated? It has recent activity (as of March 2013) and has worked great with node 0.8.

你在哪里看到xml2js已被弃用?它有最近的活动(截至 2013 年 3 月)并且与节点 0.8 配合得很好。

I use it and am happy with it!

我使用它并且很满意!

回答by mikemaccana

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

正如另一位海报指出的那样,node-xmltojs可能是最好的方法。

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:

如果您确实想使用 JSONML,我不确定为什么另一个答案中的 JQuery 插件会被点赞:nodeJSONML

npm install jsonml

Example:

例子:

var fs = require('fs'),
  parse = require('jsonml').parse;

var jsonML = parse(fs.readFileSync('myfile.xml'));

回答by andreiashu

sblompointedout JsonMLwhich might also be worth taking into consideration. Not sure about JsonML support in nodejs but there is already a jQuery plugin here.

sblom指出JsonML这也可能是值得考虑。不确定 nodejs 中的 JsonML 支持,但这里已经有一个jQuery 插件

回答by Parth Raval

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

要在 Node js 中将 XML 转换为 JSON,可以使用xml2json包。

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

要安装软件包:- 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

有关更多详细信息,请访问:- xml2json

回答by weisjohn

easyxmlis by far my favorite. I love the way it auto-pluralizes arrays.

easyxml是迄今为止我最喜欢的。我喜欢它自动复数数组的方式。