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

<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>katitis@gmail.com</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>kati@gmail.com</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>

Answers


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.

title.push(monument[k].getElementsByTagName("title")[0]);

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

alert(title[k].childNodes[0].nodeValue);

Updated the fiddle: http://jsfiddle.net/helderdarocha/4sZ8x/1/


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...