Search code examples
phpxmlnode.jssimplexml

Equivalent to PHP's simplexml_load_string in Node.js


In PHP I have

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

How would I perform the same action in a Node.js project?


Solution

  • Yes, you can use 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());
    });
    

    EDIT:

    So a research a little and you can use: dojotoolkit.org/reference-guide/1.10/dojox/xml.html that I found be more easy to use.

    Here a example: jsfiddle.net/arthurvasconcelos/gwpqyf06/