I am having an issue trying to get a specific element from an xml file. See below the javascript code i use and then the xml file. More specifically, when i retrieve the title element of the first monument element its fine. The problem is when i try to retrieve the title element of the second monument element of the xml file. The second alert shows nothing.What i do wrong? There is something wrong inside my for loop i guess.
javascript file
<script>
function loadXMLDoc(doc) {
if (window.XMLHttpRequest){
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open("GET",doc,false);
request.send("");
return request.responseXML;
}
function displaypafosmonuments(doc) {
xmlDoc = loadXMLDoc(doc);
pafos=xmlDoc.getElementsByTagName("pafos");
monuments=pafos[0].getElementsByTagName("monuments");
monument=monuments[0].getElementsByTagName("monument");
var title = new Array(3);
for(var k=0;k<2;k++){
title.push(monument[k].getElementsByTagName("title"));
alert(title[k].nodeName);
}
}
</script>
xml file
<?xml version="1.0" encoding="UTF-8"?>
<data>
<pafos>
<monuments>
<monument>
<username>dimitris</username>
<email>[email protected]</email>
<title>Colossi Castle</title>
<description>Very nice castle</description>
<image>colossi.jpg</image>
<geolocation></geolocation>
<date>08/03/2014</date>
<time>01:33</time>
</monument>
<monument>
<username>aristi</username>
<email>[email protected]</email>
<title>Castle</title>
<description>Very ugly castle</description>
<image>colossi.jpg</image>
<geolocation></geolocation>
<date>09/03/2014</date>
<time>02:33</time>
</monument>
</monuments>
</pafos>
</data>