Can't parse XML from AJAX response.

Posted by Pavel on Stack Overflow See other posts from Stack Overflow or by Pavel
Published on 2010-06-11T09:41:14Z Indexed on 2010/06/11 10:03 UTC
Read the original article Hit count: 178

Filed under:
|

Hi everyone. I'm having some problems with parsing the xml response from my ajax script. The XML looks like this:

<IMAGE>
    <a href="address">
    <img width="300" height="300" src="image.png class="image" alt="" title="LINKING"/>
    </a>
</IMAGE>
<LINK>
    www.address.com
</LINK>
<TITLE>
   This 
   <i>is title</i>
</TITLE>
<EXCERPT>
   <p>
   And some excerpt
   </p>
</EXCERPT>

The code for js looks like this.

function loadTab(id)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseXML;
var title="";
var image="";                                           

    x=xmlDoc.getElementsByTagName("TITLE");
             for (i=0;i<1;i++)
             {
             title=title + x[i].childNodes[0].nodeValue;
             }
    document.getElementById("ntt").innerHTML=title;


    x1=xmlDoc.getElementsByTagName("IMAGE");
             for (j=0;j<1;j++)
             {
             image=image + x1[j].childNodes[0].nodeValue;
             }
    document.getElementById("nttI").innerHTML=image;
 }


 }

 var url = 'http://www.factmag.com/staging/page/?id='+id;

 xmlhttp.open("GET",url,true);
 xmlhttp.send();

}

When I'm parsing it it pulls out the title but not the IMAGE tag contents. What I'm doing wrong? Can someone please tell me? Thanks in advance!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about Xml