Berikut cara membaca file XML dengan menggunakan javascript :
HTML :
<button type="button" onclick="loadXMLText()">Get XML Text</button><p id="demo1"></p><button type="button" onclick="loadXMLAttr()">Get XML Attribute</button><p id="demo2"></p>
Javascript :
function loadXMLText() {var xmlhttp = new XMLHttpRequest();xmlhttp.onreadystatechange = function() {if (this.readyState == 4 && this.status == 200) {myFunction(this);}}xmlhttp.open("GET", "../assets/source/SC_1_A/tilemapresource.xml", true);xmlhttp.send();}function myFunction(xml) {var x, i, xmlDoc, txt;xmlDoc = xml.responseXML;txt = "";x = xmlDoc.getElementsByTagName("SRS");for (i = 0; i< x.length; i++) {txt += x[i].childNodes[0].nodeValue + "<br>";}document.getElementById("demo1").innerHTML = txt;}function loadXMLAttr() {var xmlhttp = new XMLHttpRequest();xmlhttp.onreadystatechange = function() {if (this.readyState == 4 && this.status == 200) {myFunction2(this);}};xmlhttp.open("GET", "../assets/source/SC_1_A/tilemapresource.xml", true);xmlhttp.send();}function myFunction2(xml) {var x, i, xmlDoc, txt;xmlDoc = xml.responseXML;txt = "";x = xmlDoc.getElementsByTagName("Origin");for (i = 0; i < x.length; i++) {txt += "x = " + x[i].getAttribute('x') + " and y = " + x[i].getAttribute('y');}document.getElementById("demo2").innerHTML = txt;}
Terima kasih. Selamat Mencoba