在 Node.js 中解析 XML 字符串的最简单方法是什么?

问题描述:

我环顾四周,没有看到在 Node.js 中解析 XML 的明显方法.我假设有一些相对直接的 XML 对象,我可以将字符串或 url 传递给它,但我在规范中没有找到类似的东西.我需要外部库吗?如果是这样,你们会推荐哪一个?我不需要 xPath(虽然我不介意),只要我能以一种明显的方式遍历树(测试 nodeType == ElementNode 并循环遍历孩子).

I'm looking around and not seeing an obvious way to parse XML in Node. I'm assuming that there's some relatively straight forward XML object that I can pass a String or url to, but I'm not finding anything like that in the spec. Do I need an external lib? and if so, which one would you guys recommend? I don't need xPath (though I wouldn't mind it) as long as I can walk the tree in an obvious way (test if nodeType == ElementNode and loop through children).

列出了多种 XML 模块 此处,也许您可​​以找到最适合您的.我知道的一个流行的方法是 sax-js.如果您已经熟悉该库,还有 libxml 绑定.

There are a variety of XML modules listed here where perhaps you may find one that works best for you. A popular one that I'm aware of is sax-js. There are also libxml bindings if you're already familiar with that library.