Skip to content Skip to sidebar Skip to footer

How Do I Read Xml In Javascript?

Alright, so I got this xml file: 10000000000000011

Solution 1:

I'd use XMLHttpRequest and its responseXML property instead, which will work in all major browsers. Example:

functionreadLevel(xmlDoc) {
    alert(xmlDoc.documentElement.tagName);
    // Your existing code goes here
};

var createXmlHttpRequest = (function() {
    var factories = [
        function() { returnnewXMLHttpRequest(); },
        function() { returnnewActiveXObject("Msxml2.XMLHTTP.6.0"); },
        function() { returnnewActiveXObject("Msxml2.XMLHTTP.3.0"); },
        function() { returnnewActiveXObject("Microsoft.XMLHTTP"); }
    ];

    for (var i = 0, len = factories.length; i < len; ++i) {
        try {
            if ( factories[i]() ) return factories[i];
        } catch (e) {}
    }
})();

var xmlHttp = createXmlHttpRequest();
xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        readLevel(xmlHttp.responseXML);
    }
};

xmlHttp.open("GET", "levels/test.xml", true);
xmlHttp.send(null);

Solution 2:

According to SitePoint, all the arguments are required in createDocument. Maybe the falsy values are tripping you up.

Post a Comment for "How Do I Read Xml In Javascript?"