等同于Node.js中的PHP simplexml_load_string

在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

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

 // 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()); });