Q:

AJAX: responseXML returning null however ResponseText returns the XML as a string

I'm trying to retrieve an XML file from the server-side (PHP) and subsequently send it to the client-side (JavaScript). The issue i'm having is that the responseXML keeps returning null. However, if i change it to responseText it returns the xml file in a string so this tells me that the .js can receive the xml file from the server side.

cart.js
if (window.XMLHttpRequest)
{ 
  xHRObject = new XMLHttpRequest();
}

else if (window.ActiveXObject)
{ 
  xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function loadDoc() 
{

  xHRObject.open("GET", "cart.php", true);

  xHRObject.onreadystatechange = function() 
  {      
  if (xHRObject.readyState == 4 && xHRObject.status == 200) 
  {        
    myFunction(xHRObject);
  }

  }

  xHRObject.send();
}

function myFunction(xml) 
{  
  var xmlDoc = xml.responseXML;

  alert(xmlDoc);

 ...

}

cart.php
<?php 

 header('Content-Type: text/xml');

 $xmlFile = "cart.xml";

  $doc = new DOMDocument();
  $doc->load($xmlFile);

  $strXml = $doc->saveXML(); 
  echo $strXml;

?>

Hope someone here could help me out as i've search this problem online but couldn't find a solution. I've also tried parsing the responseText:
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(XHR.responseText, "text/xml");

but it can never return the nodes.
Any help will be appreciated!
Thank you
Solution: had blank line at the top of php file

A:

The problem was that php file had a blank line at the top. Had to remove it so no extra (unnecessary) content will be printed out.

