can't retrieve specific element from from xml file using javascript

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

    function loadXMLDoc(doc) { 
        if (window.XMLHttpRequest){ 
            request = new XMLHttpRequest(); 
        } else { 
            request = new ActiveXObject("Microsoft.XMLHTTP"); 
        return request.responseXML; 

    function displaypafosmonuments(doc) {
        xmlDoc = loadXMLDoc(doc);


        var title = new Array(3);
for(var k=0;k<2;k++){

xml file

<?xml version="1.0" encoding="UTF-8"?>
                <title>Colossi Castle</title>
                <description>Very nice castle</description>
                <description>Very ugly castle</description>


You are creating an array with three empty places, and then inside the loop you push two more items. Now your array has five elements. But you read the first and the second, which are null.

One way to fix it is to change your array declaration:

var title = new Array();

Now you will be pushing elements 0 and 1, and reading elements 0 and 1.

Inside the loop you are reading the title node-set. You have to get the first (and only) title element.


Finally, your alert() is not accessing the text node. So this will work:


Updated the fiddle:

Need Your Help

Managing internal 3rd Party Dependencies

visual-studio version-control projects-and-solutions dependency-management

We have a lot of different solutions/projects which are managed by different teams. Our solution needs to reference several projects that another team owns. We don't want to add these dependencies as

UDP Client Server Java

java udp client-server

I have simple UDP client and server codes. When the server gets the request from the client, I want to form a reply out of the data send by the client together with some additional data, as shown i...