0

I don't know if I'm having a syntax error but the compiler is giving me

TypeError: 'undefined' is not an object (evaluating 'xmlDoc.getElementsByTagName("icon")[count].childNodes')

Its me giving me this problem when im parsing the XML from my server, my actual javascript code is like this

var xmlDoc = Obj.responseXML;
var count = 0;
if(xmlDoc){
    while(count <= xmlDoc.getElementsByTagName("item").length){
    document.getElementById("flow").innerHTML += "<div class='item'><img class='content' src='" + xmlDoc.getElementsByTagName("icon")[count].childNodes[0].nodeValue.replace(/\s+$/g,' ') +"' /></div>";
    count++;
    }
       }else{
           alert("Unable to parse!");
       }

and my XML goes like this.

<feed>
<item>
<title>Given Title</title>
<icon>
http://i178.photobucket.com/albums/w255/ace003_album/Logo-ETC-RGB-e1353503652739.jpg
</icon>
</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
</feed>

i just want to parse the image link and to show it. DOM parser

 var url = "http://myURL.com/document.xml";
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          Obj = new XMLHttpRequest();
          }
        else
          {
          Obj = new ActiveXObject("Microsoft.XMLHTTP");
          }

        Obj.open("POST",url,false);
        Obj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        Obj.send();
3
  • 1
    check what i is, what is probably happening is you are trying to access an icon element that doesn't exist in the node list. Commented Oct 25, 2013 at 15:51
  • so sorry, I dont know whats that :) Commented Oct 25, 2013 at 16:20
  • 1
    @Ace Munim Give a look js-xpath.sourceforge.net/xpath-example.html and npmjs.org/package/xpath Commented Oct 25, 2013 at 16:38

1 Answer 1

1

Demo

Firstly, you're while loop condition should be just < not <=. By using the latter, the loop is running one too many times, causing the error because the index is out of bounds.

Secondly, in the while loop you're getting the icon elements based on the count from the root of the document. The icon's are children of each item so you should retrieve the icon relative to the item by using item.getElementsByTagName('icon')[0] not xmlDoc.getElementsByTagName('icon')[count].

Not related to the problem but building HTML as strings like that is undesirable. Creating the elements and inserting them into the DOM would be better because you don't need to handle any escaping. Also, store a reference to flow before the while, instead of finding it on each iteration.

var div;
var img;
var flow = document.getElementById('flow');
var items = xmlDoc.getElementsByTagName("item");

while(count < items.length){
    div = document.createElement('div');
    div.className = 'item';

    img = document.createElement('img');
    img.className = 'content';
    img.src = items[count].getElementsByTagName("icon")[0].childNodes[0].nodeValue.replace(/\s+$/g,' ');

    div.appendChild(img);
    flow.appendChild(div);
    count++;
}
Sign up to request clarification or add additional context in comments.

4 Comments

i did change and all, and sorry the variable i with count was a typo, and also I've implemented the method you told me as it is much better in creating element. But still im having the same error. Is there any way to check whether I'm getting the proper XML?
yup it is, added the DOM parser in the question.
im getting the full XML from my site with the Obj.responseXML
See my edit. There's a couple of problems. Also see the demo (you don't need the DOMParser that I have in the fiddle but it's needed for the demo).

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.