I am trying to use jQuery parseXml in node.js
我正在尝试在node.js中使用jQuery parseXml
I am getting this error:
我得到了这个错误:
Error: Invalid XML: <?xml version="1.0"...
But the problem is not in the XML
但问题不在于XML
The problem is in node-jquery.js:
问题出在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" )
简单地说,在node。没有DOMParser,也没有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:
因为我在windows中工作,所以我希望ActiveXObject能够正常工作,但是,jQuery所犯的错误并不是无效的XML,而是ActiveXObject没有被定义:
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?
任何解决方法吗?如何制作jQuery。parseXML工作吗?
3 个解决方案
#1
6
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.
我已经成功地使用了xmldom。看一看。它解析xml就像您期望的$一样。parseXML。我在jquery解析器上也遇到了问题,在尝试了很多之后,我转向了这个。
#2
5
If you want to continue to use jQuery.parseXML
without having to make any changes to your code (like if you want to be able to run the same javascript both client-side and in Node.js), it is easy to set that up. The xmldom module mentioned in M. Laing’s answer exposes a DOMParser
constructor that jQuery can use the same way it uses DOMParser in the browser. Here’s how to do so:
如果你想继续使用jQuery。parseXML无需对代码做任何修改(比如如果您希望能够同时在客户端和Node.js中运行相同的javascript),设置它就很容易。M. Laing的回答中提到的xmldom模块公开了一个DOMParser构造函数,jQuery可以使用与在浏览器中使用DOMParser相同的方式。如何做到这一点:
First, install xmldom:
首先,安装xmldom:
npm install xmldom --save
Assuming you already have jQuery 2.1.x or greater working in Node.js (for instructions on setting that up, see the README in this repository), you can now just require xmldom and expose its DOMParser
constructor as a global:
假设您已经有jQuery 2.1。在节点中工作的x或更多。js(关于设置的说明,请参阅这个存储库中的README),您现在只需使用xmldom并将其DOMParser构造函数公开为全局:
global.DOMParser = require('xmldom').DOMParser;
jQuery will now be able to successfully parseXML
.
jQuery现在可以成功解析sexml了。
#3
1
That looks to be something that would have to be implemented into the core of nodejs. I would suggest using a module designed to parse XML.
这似乎是必须在node . js核心中实现的东西。我建议使用一个用于解析XML的模块。
https://github.com/Leonidas-from-XIV/node-xml2js
https://github.com/Leonidas-from-XIV/node-xml2js
Do you need jQuery.parseXML to work, like are you trying to write code to cross into the browser and run on the server?
你需要jQuery。parseXML要工作,比如您是否正在尝试编写代码,以便跨到浏览器并在服务器上运行?
You could probably expose node-xml2js in the browser with browserify
您可以在浏览器中使用browserify公开node-xml2js
There is also libxmljs which seems to be more XML like than node-xml2js.
还有libxmljs,它看起来更像XML,而不是node-xml2js。
#1
6
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.
我已经成功地使用了xmldom。看一看。它解析xml就像您期望的$一样。parseXML。我在jquery解析器上也遇到了问题,在尝试了很多之后,我转向了这个。
#2
5
If you want to continue to use jQuery.parseXML
without having to make any changes to your code (like if you want to be able to run the same javascript both client-side and in Node.js), it is easy to set that up. The xmldom module mentioned in M. Laing’s answer exposes a DOMParser
constructor that jQuery can use the same way it uses DOMParser in the browser. Here’s how to do so:
如果你想继续使用jQuery。parseXML无需对代码做任何修改(比如如果您希望能够同时在客户端和Node.js中运行相同的javascript),设置它就很容易。M. Laing的回答中提到的xmldom模块公开了一个DOMParser构造函数,jQuery可以使用与在浏览器中使用DOMParser相同的方式。如何做到这一点:
First, install xmldom:
首先,安装xmldom:
npm install xmldom --save
Assuming you already have jQuery 2.1.x or greater working in Node.js (for instructions on setting that up, see the README in this repository), you can now just require xmldom and expose its DOMParser
constructor as a global:
假设您已经有jQuery 2.1。在节点中工作的x或更多。js(关于设置的说明,请参阅这个存储库中的README),您现在只需使用xmldom并将其DOMParser构造函数公开为全局:
global.DOMParser = require('xmldom').DOMParser;
jQuery will now be able to successfully parseXML
.
jQuery现在可以成功解析sexml了。
#3
1
That looks to be something that would have to be implemented into the core of nodejs. I would suggest using a module designed to parse XML.
这似乎是必须在node . js核心中实现的东西。我建议使用一个用于解析XML的模块。
https://github.com/Leonidas-from-XIV/node-xml2js
https://github.com/Leonidas-from-XIV/node-xml2js
Do you need jQuery.parseXML to work, like are you trying to write code to cross into the browser and run on the server?
你需要jQuery。parseXML要工作,比如您是否正在尝试编写代码,以便跨到浏览器并在服务器上运行?
You could probably expose node-xml2js in the browser with browserify
您可以在浏览器中使用browserify公开node-xml2js
There is also libxmljs which seems to be more XML like than node-xml2js.
还有libxmljs,它看起来更像XML,而不是node-xml2js。