Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make jQuery.parseXML work in node.js

I am trying to use jQuery parseXml in node.js

I am getting this error:

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

But the problem is not in the XML

The problem is in 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;
    },

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

Since I am working in windows, I would expect ActiveXObject to work, but no, it does not, the actual error swallowed by jQuery is not an invalid XML it is 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 make jQuery.parseXML work?

like image 276
Luxspes Avatar asked Jul 27 '12 23:07

Luxspes


People also ask

How to parse XML in jQuery?

The parseXML() method in jQuery is used to parse a string into an XML document. It uses native methods of the browser for creating a valid XML document. This valid XML document can be passed to jQuery for creating a jQuery object that can be manipulated or traversed.

How do you access XML elements in node JS?

Node. js has no inbuilt library to read XML. The popular module xml2js can be used to read XML. The installed module exposes the Parser object, which is then used to read XML.

Does jQuery work with XML?

jQuery can be used for XML processing on the Web as well as HTML processing, and in this article I show some examples of this use.

What is XML node JS?

Using the XML Parsing in NodeJS XML parsing is the process of reading and modifying XML data so that client applications can efficiently work with the data. Besides reading XML documents, parsers also verify that a given XML document conforms to the standard XML syntax and checks for any document violations or errors.


1 Answers

I've had great success using xmldom. Take a look. It seems to parse an xml just like you would expect $.parseXML to. I was also having problems with the jquery parser and switched to this one after trying a bunch out.

like image 106
M. Laing Avatar answered Sep 22 '22 10:09

M. Laing