Parse XML in Mootools

前端 未结 2 1789
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 05:45

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

相关标签:
2条回答
  • 2021-01-01 06:10

    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
    }
    
    0 讨论(0)
  • 2021-01-01 06:20

    In MooTools Forge there is a plugin for converting XML to a JavaScript object:

    http://mootools.net/forge/p/xml2js_converter

    0 讨论(0)
提交回复
热议问题