相当于 PHP 的 simplexml_load_string 在 Node.js

Equivalent to PHP's simplexml_load_string in Node.js

在 PHP 我有

$this->response->body = simplexml_load_string($this->response->body);

如何在 Node.js 项目中执行相同的操作?

是的,你可以使用 jsdom

WHATWG DOM 和 HTML 标准的 JavaScript 实现,用于 Node.js.

// Run some jQuery on a html fragment
var jsdom = require('jsdom');

jsdom.env('<p><a class="the-link" href="https://github.com/tmpvar/jsdom">jsdom\'s Homepage</a></p>', [
  'http://code.jquery.com/jquery-1.5.min.js'
],
function(errors, window) {
  console.log("contents of a.the-link:", window.$("a.the-link").text());
});

是的,您可以使用 jsdom as @florian-margaine exemplified here PHP's SimpleXMLElement analog for Node.js

// Run some jQuery on a html fragment
var jsdom = require('jsdom');

jsdom.env('<p><a class="the-link" href="https://github.com/tmpvar/jsdom">jsdom\'s Homepage</a></p>', [
  'http://code.jquery.com/jquery-1.5.min.js'
],
function(errors, window) {
  console.log("contents of a.the-link:", window.$("a.the-link").text());
});

编辑:

所以稍微研究一下,你可以使用:dojotoolkit.org/reference-guide/1.10/dojox/xml.html 我发现它更容易使用。

这里有一个例子:jsfiddle.net/arthurvasconcelos/gwpqyf06/