在 Mootools 中解析 XML

发布于 2024-11-30 14:52:31 字数 2189 浏览 0 评论 0原文

似乎没有任何关于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒗幽 2024-12-07 14:52:31

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

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

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 这可能更有效:

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
}

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 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
}

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
}
温柔嚣张 2024-12-07 14:52:31

在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文