AS3 cant read xml dynamically generated with PHP

THIS PROBLEM HAS BEEN RESOLVED - THE SOLUTION IS A FEW POSTS DOWN

hey all - I´ve been trying to write AS3 that can read xml which is dynamically generated by php. i´ve generated the xml in several ways – all of which seem to work but i keep getting the error,

Error #1088: The markup in the document following the root element must be well-formed,

when i try to load the xml into flash. however the xml (as viewed when you go to “view source” in your browser) is indeed well formed.

i´ve seen various discussions where people bring up this problem but haven´t seen a solution in any of them.

i have 4 versions of the php. the first just uses echo. the second uses echo but i wrap it in {start_ob(),ob_end_flush()}; then i use simpleXML and finally i use domdocument. the first 3 give the error above, the domdocument throws no error but the xml is empty.

i´ll start with the simplified flash code:


var path:String = "php/videos.php"
//
var ldr:URLLoader = new URLLoader()
ldr.addEventListener(Event.COMPLETE,xloaded)
//
var req:URLRequest = new URLRequest(path)
ldr.load(req)
//
function xloaded(e:Event):void{
	trace("loaded",e.target)
    trace(e.target.data)
	var xml:XML = new XML(e.target.data)
	trace("the xml:  ",xml)
}

php: echo version



$files = "../video";
$thedirectory = @opendir($files) or die("Unable to open $files");

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
";
echo "<files>
";
while ($file = readdir($thedirectory)) {

if ($file!="." && $file!="..") {
     echo "<file>".$file."</file>
";
};       
					
};

echo "</files>";

closedir($thedirectory);

?> 

php: echo 2



ob_start();

$files = "../video";
$thedirectory = @opendir($files) or die("Unable to open $files");

$thexml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
";
$thexml .= "<files>
";


while ($file = readdir($thedirectory)) {

if ($file!="." && $file!="..") {
                   
				   $thexml .= "<file>".$file."</file>
";
					
 };

};

closedir($thedirectory);


$thexml .= "</files>
";


echo $thexml;

header("content-type: text/xml");
header("Content-Length: ". ob_get_length());



ob_end_flush();


php: simpleXML


header('Content-Type: text/xml; charset=utf-8');

$xmlstring = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<files>
</files>
XML;
$xml = new SimpleXMLElement($xmlstring);

$files = "../video";
$thedirectory = @opendir($files) or die("Unable to open $files");

while ($file = readdir($thedirectory)) {

if ($file!="." && $file!="..") {
                  
       $xml -> addChild("file",$file);
					
 };

};

echo $xml->asXML();

php: domdocument (i´ve also tried asXML() in place of saveXML() in the below echo.


header ("content-type: text/xml");

$dom = new DomDocument('1.0');
$dom->formatOutput = true;

$root = $dom->createElement('files');
$dom->appendChild($root);


$files = "../video";
$thedirectory = @opendir($files) or die("Unable to open $files");

while ($file = readdir($thedirectory)) {

if ($file!="." && $file!="..") {
      $child = $dom->createElement('file');
	  $text = $dom->createTextNode($file);

      $root->appendChild($child);
	  $child->appendChild($text);
	        					
};

};

echo $dom->saveXML();

closedir($thedirectory)