对于 Node.js 的 xml 到 json 有什么建议吗?
我已经安装了node-xml,但我认为它没有按我预期的方式工作。而且它没有例子。对于node.js 的 xml-2-json (js) 有什么建议吗?我还在 npm 中查看了 xml2js,但它已被弃用,人们报告说它与最新的 node.js 一起被破坏了
,顺便说一句,我也在使用express。 :)
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. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
有许多 xml 解析器。
就像 libxmljs 和 node-o3-xml。后者是由 Ajax.org 制作和使用的,因此它应该是稳定的。
至于将 XML 转换为 JSON,我建议从 xml 创建一个对象结构,然后在其上手动调用
JSON.stringify
。这使您可以完全控制 xml 数据如何转换为 JSON。然后,您可以将 JSON 保存在文件/数据库中或将其提供给请求。
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.
libxmljs 和 node-o3-xml 很棒而且速度很快,但要注意它们都需要编译二进制组件。如果您将它们用于其他人将使用的模块,那么这个警告就更严重了。
从更高层次的角度来看,请记住节点是单线程的。因此,您所做的任何 XML 解析都会阻塞节点进程。对我来说,这意味着 XML 解析永远不应该在与主应用程序相同的进程中执行。然后,一旦将 XML 解析移至单独的进程,也许可以牺牲一点速度来换取易于安装和更大的可移植性。
就我个人而言,这就是为什么我在我的 sax.js - 一个纯 JavaScript SAX 解析器 - a href="https://github.com/danmactough/node-feedparser" rel="noreferrer">feedparser 库(如果您正在解析 RSS/Atom/RDF 提要,请考虑尝试它 - 评论并且非常欢迎拉取请求)。老实说,在解析像 RSS 提要这样大的内容时,sax.js 和 libxmljs 之间没有明显的速度差异。我想,如果您正在解析巨大的 XML 文件,您可能会注意到其中的差异。但即便如此,sax.js 的一大好处就是流媒体。与 libxmljs(我上次使用它)不同,您可以将流通过管道传输到 sax.js,而不必将整个 XML 文档读入内存。如果您正在解析巨大的文件,您一定会喜欢它!
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!
您在哪里看到 xml2js 已弃用?它最近有活动(截至 2013 年 3 月)并且与 Node 0.8 配合得很好。
我使用它并且很满意!
Where do you see that xml2js is deprecated? It has recent activity (as of March 2013) and has worked great with node 0.8.
I use it and am happy with it!
Chekout http://hemanth.github.com/node-rsj/
Chekout http://hemanth.github.com/node-rsj/
正如另一张海报指出的那样, node-xmltojs 可能是最好的方法。
如果您确实想使用 JSONML,我不确定为什么另一个答案中的 JQuery 插件被投票:有 节点的 JSONML:
示例:
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:
Example:
sblom 指出 JsonML 这也可能值得考虑。不确定 NodeJS 中的 JsonML 支持,但这里已经有一个 jQuery 插件。
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.
要在 Node js 中将 XML 转换为 JSON,可以使用 xml2json 包。
要安装包:-
npm install --save xml2json
添加代码片段:-
有关更多详细信息,请访问:- xml2json
To convert XML to JSON in Node js, you can use xml2json package.
To install the package:-
npm install --save xml2json
Add Code snippet:-
For more details please visit:- xml2json
easyxml 是迄今为止我最喜欢的。我喜欢它自动复数数组的方式。
easyxml is by far my favorite. I love the way it auto-pluralizes arrays.