Parse XML in Mootools

a 夏天 提交于 2019-11-30 10:10:38

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
}

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!