JSFiddle - React, Tailwind, and code Playground
by phloe
HTML
<body onload="process()">
Here is a list of people from the XML file.
<br/>
<br/>
<div id="theD"></div>
</body>
JavaScript
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}else{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}
function process() {
if (xmlHttp) {
try{
xmlHttp.open("POST", "/echo/xml/", true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send("xml=<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><datafile><people><person><name>John Doe</name><ssn>xxx-xx-xxxx</ssn></person></people></datafile>&delay=3");
} catch (e) {
alert ("In process function.<br/>Error in creating xmlHttp object: "+ e.toString());
}
}
}
function handleStateChange() {
if(xmlHttp.readyState==4) {
if (xmlHttp.status==200) {
try {
handleResponse();
}
catch(e) {
alert ("Trouble getting text." + e.toString());
}
} else {
alert ("State = "+xmlHttp.readyState+" Status= " + xmlHttp.status);
}
}
}
function handleResponse() {
var xmlResponse = xmlHttp.responseXML,
root = xmlResponse.documentElement,
names = root.getElementsByTagName("name"),
ssns = root.getElementsByTagName("ssn");
alert (xmlHttp.responseText);
var stuff = "";
for(var i=0;i<names.length;i++) {
stuff = names.item(i).firstChild.data + "-" + ssns.item(i).firstChild.data + "<br/>";
}
theD = document.getElementById("theD");
theD.innerHTML = stuff;
}