0

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>

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

7 Comments

thanks a lot! you are right. my actual problem is when i try to do this in a for loop. can you check my edited post and tell me what is wrong now? i have the same issue in the for loop
I see. You are pushing two more elements on an array of size 3 and then reading the first two, which would be null. You also aren't adding the title, but the node-set. I'll edit the answer.
thanks again. i will use firebug from now on, i am new to javascript and xml.
but i don't push more elements. i removed the other lines for adding elements to that array and i replaced them by adding the elements inside the for loop
Check the fiddle link I posted. When you post CSS, JavaScript and HTML questions in Stack Overflow, always add a JSFiddle since that makes it easier for us to test it. It's usually possible to adapt the problem to fit there.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.