相当于 PHP 在 Node.js 中的simplexml_load_string


Equivalent to PHP's simplexml_load_string in Node.js

在PHP中,我有

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

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

是的,你可以使用 jsdom 作为 @florian-margaine,这里的例子是 PHP 的 SimpleXMLElement 模拟 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/

是的,你可以使用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());
});