I’m working on importing XML data to a webpage, using javascript function. I’ve gotten it to work, mostly. The problem is this: I can’t get it to write all the data in the XML file.
Here’s an exerpt from an XML file I’m using:
<program>
<character>
<named>Kirk</named>
<rank>Captain</rank>
<show>Star Trek</show>
</character>
<character>
<named>Spock</named>
<rank>Commander</rank>
<show>Star Trek</show>
</character>
<character>
<named>McCoy</named>
<rank>Commander</rank>
<show>Star Trek</show>
</character>
</program>
Here’s my javascript function:
function getTheFile(theFile){
xmlhttp.open("GET",theFile,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
person=xmlDoc.getElementsByTagName("character");
for(i=0;i<person.length; i++){
char=(person*.getElementsByTagName("named")[0].childNodes[0].nodeValue);
ranked=(person*.getElementsByTagName("rank")[0].childNodes[0].nodeValue);
shown=(person*.getElementsByTagName("show")[0].childNodes[0].nodeValue);
txt="<h2>" +char + "</h2><p>" + ranked + "<br />"+ shown +"</p>";
document.getElementById("showGuy").innerHTML=txt;
}
}
</script>
The end result is - the function writes the last group of tags and nothing else:
McCoy
Commander
Star Trek
For the life of me, I cannot figure out how to write everything else. Help me! Thanks