简体   繁体   中英

Can't parse XML from AJAX response

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!

You don't have XML there, an XML document can have only one root node, so anything following </IMAGE> is an error.

You probably want to wrap your document with a new element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM