在Mootools中解析XML [英] Parse XML in Mootools

查看:64
本文介绍了在Mootools中解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mootools中解析XML似乎没有任何有用的文档。无论是如此愚蠢容易,都没有人可以提起它,或者每个人放弃尝试都是如此极其困难。有没有人有任何简单的跨浏览器方法来使用Mootools解析XML?

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?

这是我的小XML文件data.xml:

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>

这是我的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')。

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.

任何善良的专家能否恢复我对Mootools神秘力量的信心?

Can any kindly expert restore my faith in the mystical powers of Mootools?

干杯
Fred

Cheers Fred

推荐答案

这是一个相当古老的问题,但我最近遇到了同样的问题,所以我想分享我的解决方案。

This is a rather old question but I recently had the same problem, so I'd like to share my solution.

Request 收到的 responseXML 变量只是来自浏览器的未更改的XML响应。在IE(最高版本9)下,您将收到一个IXMLDOMDocument对象。我发现将此对象转换为MooTools 元素树的最简单方法如下:

The responseXML variable you receive from Request 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 MooTools Element tree is the following:

function(responseText, responseXML) {
    var doc = responseXML.documentElement;
    if (doc.xml) {
        doc = new Element('div', { html: doc.xml }).getFirst();
    }
    // Now you can use 'doc' like any other MooTools Element
}

或者,您可以使用IE的 DOMParser ,这可能更有效:

Alternatively, you can use IE's DOMParser which might be more efficient:

function(responseText, responseXML) {
    var doc = responseXML.documentElement;
    if (doc.xml) {
        var parser = new DOMParser();
        var html = parser.parseFromString(doc.xml, 'text/xml');
        doc = document.id(html.documentElement);
    }
    // Now you can use 'doc' like any other MooTools Element
}

这篇关于在Mootools中解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆