How can I get jQuery.parseXML to work in node.js

I am trying to use jQuery parseXml in node.js

I get this error:

Error: Invalid XML: <?xml version="1.0"...

But the problem is not XML

The problem is node-jquery.js:

parseXML: function( data ) {
        if ( typeof data !== "string" || !data ) {
            return null;
        }
        var xml, tmp;
        try {
            if ( window.DOMParser ) { // Standard
                tmp = new DOMParser();
                xml = tmp.parseFromString( data , "text/xml" );
            } else { // IE
                xml = new ActiveXObject( "Microsoft.XMLDOM" );
                xml.async = "false";
                xml.loadXML( data );
            }
        } catch( e ) {
            xml = undefined;
        }
        if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) {
            jQuery.error( "Invalid XML: " + data );
        }
        return xml;
    },

Simply put, there is no DOMParser in node.js and no ActiveXObject ("Microsoft.XMLDOM")

Since I work on Windows, I would expect ActiveXObject to work, but no, it is not, the actual error swallowed by jQuery is not invalid XML, this means that ActiveXObject is not defined:

ReferenceError: ActiveXObject is not defined
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:

Any workarounds for this? How can I get jQuery.parseXML to work?

+5
source share
3 answers

, xmldom. . , xml , $.parseXML. jquery , .

+6

jQuery.parseXML, (, javascript , Node.js), . xmldom, M. "" DOMParser, jQuery , DOMParser . :

xmldom:

npm install xmldom --save

, jQuery 2.1.x , Node.js( . README ), xmldom DOMParser :

global.DOMParser = require('xmldom').DOMParser;

jQuery parseXML.

+5

This is similar to what should be implemented in the nodejs core. I would suggest using a module designed for XML parsing.

https://github.com/Leonidas-from-XIV/node-xml2js

Do you need jQuery.parseXML to work, for example, are you trying to write code to go to the browser and run on the server?

Perhaps you can in node-xml2js in browserify browser

There is also libxmljs , which appears to be more XML than node-xml2js.

+1
source

All Articles