Friday, 3 April 2015

XML Parser JavaScript and jQuery Ajax

XML File sample.xml

 <ACTIVITY>
    <title>Hellow</title>
</ACTIVITY>

JavaScript: 

if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        }
else // code for IE5 and IE6
        {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
xhttp.open("GET", "sample.xml", false);
xhttp.send();
xmlDoc = xhttp.responseXML;
alert(xmlDoc.getElementsByTagName("title")[0].innerHTML);


jQuery Ajax: 


$.ajax({
            type: "GET",
            url: "sample.xml",
            dataType: "xml",
            success: function(xml) {         
                var xmlDoc = $.parseXML(xml.activeElement.outerHTML),
                    $xml = $(xmlDoc);
                    alert($xml.find("title").text());
            },
            error: function() {
                alert(" Please refresh: Problem in loading xml.");
            }
        });

No comments:

Post a Comment