在 Mootools 中解析 XML
似乎没有任何关于在 Mootools 中解析 XML 的有用文档。要么它太简单了,没有人愿意提及它,要么它太难了,每个人都放弃了尝试。有没有人有任何简单的跨浏览器方法来使用 Mootools 解析 XML?
这是我的小 XML 文件 data.xml:
<?xml version="1.0"?>
<suggestions>
<suggestion winning="Y">
<copy><![CDATA[Draw straws to see who wins]]>
</copy>
<person><![CDATA[Sue]]>
</person>
<location><![CDATA[London]]>
</location>
</suggestion>
<suggestion winning="N">
<copy><![CDATA[Race your friends round the meeting room]]>
</copy>
<person><![CDATA[Jack]]>
</person>
<location><![CDATA[Lancaster]]>
</location>
</suggestion>
</suggestions>
这是我的 JS:
window.addEvent('domready', function(){
var outputHTML = '';
var req = new Request({
url: 'data.xml',
method: 'get',
onSuccess: function(responseText, responseXML) {
if(typeOf(responseXML) != 'document'){
responseXML = responseXML.documentElement;
}
var suggestions = responseXML.getElements('suggestion');
suggestions.each(function(item) {
outputHTML += '<p>';
outputHTML += item.getElement('copy').get('text') + '<br/>';
outputHTML += '<b>' + item.getElement('person').get('text') + '</b>: ';
outputHTML += item.getElement('location').get('text') + '<br/>';
if (item.get('winning') == 'Y') {
outputHTML += ' <b>Won!</b>';
}
outputHTML += '</p>';
});
$('output').set('html', outputHTML);
}
}).send();
});
我发现我必须执行 responseXML = responseXML.documentElement
位才能使其在 Chrome 中工作。该 JS 在 Chrome 和 FF 中工作正常,但 IE 在第 16 行抱怨“对象不支持此属性或方法”,其中我尝试在 responseXML 上运行 getElements('suggestion')。
有哪位好心的专家可以恢复我对 Mootools 神秘力量的信心吗?
干杯 弗雷德
There doesn't seem to be any useful documentation out there about parsing XML in Mootools. Either it's so stupidly easy nobody could be bothered to mention it, or it's so fiendishly difficult everybody's given up trying. Does anyone have any simple cross browser method for parsing XML with Mootools?
Here's my little XML file data.xml:
<?xml version="1.0"?>
<suggestions>
<suggestion winning="Y">
<copy><![CDATA[Draw straws to see who wins]]>
</copy>
<person><![CDATA[Sue]]>
</person>
<location><![CDATA[London]]>
</location>
</suggestion>
<suggestion winning="N">
<copy><![CDATA[Race your friends round the meeting room]]>
</copy>
<person><![CDATA[Jack]]>
</person>
<location><![CDATA[Lancaster]]>
</location>
</suggestion>
</suggestions>
And this is my JS:
window.addEvent('domready', function(){
var outputHTML = '';
var req = new Request({
url: 'data.xml',
method: 'get',
onSuccess: function(responseText, responseXML) {
if(typeOf(responseXML) != 'document'){
responseXML = responseXML.documentElement;
}
var suggestions = responseXML.getElements('suggestion');
suggestions.each(function(item) {
outputHTML += '<p>';
outputHTML += item.getElement('copy').get('text') + '<br/>';
outputHTML += '<b>' + item.getElement('person').get('text') + '</b>: ';
outputHTML += item.getElement('location').get('text') + '<br/>';
if (item.get('winning') == 'Y') {
outputHTML += ' <b>Won!</b>';
}
outputHTML += '</p>';
});
$('output').set('html', outputHTML);
}
}).send();
});
I found I had to do the responseXML = responseXML.documentElement
bit to make it work in Chrome. This JS works OK in Chrome and FF, but IE complains "Object doesn't support this property or method" for line 16, where I'm trying to run getElements('suggestion') on responseXML.
Can any kindly expert restore my faith in the mystical powers of Mootools?
Cheers
Fred
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个相当老的问题,但我最近遇到了同样的问题,所以我想分享我的解决方案。
您从
Request
收到的responseXML
变量只是来自浏览器的未更改的 XML 响应。在 IE(最高版本 9)下,您将收到一个 IXMLDOMDocument 对象。我发现将此对象转换为 MooToolsElement
树的最简单方法如下:或者,您可以使用 IE 的
DOMParser
这可能更有效:This is a rather old question but I recently had the same problem, so I'd like to share my solution.
The
responseXML
variable you receive fromRequest
is simply the unaltered XML response from your browser. Under IE (up to version 9), you'll receive an IXMLDOMDocument object. I found that the easiest way to convert this object to a MooToolsElement
tree is the following:Alternatively, you can use IE's
DOMParser
which might be more efficient:在 MooTools Forge 中,有一个用于将 XML 转换为 JavaScript 对象的插件:
http://mootools.net/forge/ p/xml2js_converter
In MooTools Forge there is a plugin for converting XML to a JavaScript object:
http://mootools.net/forge/p/xml2js_converter