PDA

View Full Version : AS3 cant read xml dynamically generated with PHP



brookisme
February 26th, 2009, 01:41 PM
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\"?>\n";
echo "<files>\n";
while ($file = readdir($thedirectory)) {

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

};

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\"?>\n";
$thexml .= "<files>\n";


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

if ($file!="." && $file!="..") {

$thexml .= "<file>".$file."</file>\n";

};

};

closedir($thedirectory);


$thexml .= "</files>\n";


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)

ofeet
February 26th, 2009, 01:53 PM
well formed refers to the syntax being perfect. ie. <openTag></closeTag>. Maybe a tag isn't being closed properly? When viewing your xml for a browser does it parse correcly? Mind pasting the xml maybe?

brookisme
February 26th, 2009, 02:13 PM
well formed refers to the syntax being perfect. ie. <openTag></closeTag>. Maybe a tag isn't being closed properly? When viewing your xml for a browser does it parse correcly? Mind pasting the xml maybe?

thanks - yeah i keep rechecking the xml and it seems fine below are the links to all the examples. obviously you have to go to view source. videos1.php (the simpleXML example)
is all in one line up but i don´t think that should make a difference.

http://stickandlog.com/qvids/php/videos.php
http://stickandlog.com/qvids/php/videos1.php
http://stickandlog.com/qvids/php/videos2.php
http://stickandlog.com/qvids/php/videos3.php

justkevin
February 26th, 2009, 02:21 PM
Does it work if you skip the PHP and just load the file from an XML document on the server, e.g.:

http://stickandlog.com/qvids/php/myxml.xml

brookisme
February 26th, 2009, 02:36 PM
Does it work if you skip the PHP and just load the file from an XML document on the server, e.g.:

http://stickandlog.com/qvids/php/myxml.xml

yep. i just tested it out. its fine with a real xml file.

brookisme
February 26th, 2009, 02:46 PM
I just figured it out but i don´t really understand the solution. i´d love feedback on why this works:

in my actionscript code i put

var path:String = "php/videos.php"

this doesn´t work (but does with php/test.xml - test.xml being a static xml file). however
if i use

var path:String = "http://stickandlog.com/qvids/php/videos.php"

it works perfectly!

¿¿¿ any ideas on why this fix fixed ???

pakage
November 3rd, 2009, 06:32 AM
old thread i know, but yep, i just had this problem recently.

i was running WAMP on my computer to create a local server to develp on.

lets say my flash was stored in:

C:\wamp\sites\testwebsite\myflash.fla

and the xml was in:

C:\wamp\sites\testwebsite\xml\myxml.php

in flash, if i set the XML URL to a relative location eg. "xml/myxml.php"

flash interprets this relative URL to be equal to

C:\wamp\sites\testwebsite\xml\myxml.php

and rightly so, because thats where the path ive entered points to relative to where the flash is running from.

this works fine when the XML is static as no processing needs to be run on a normal XML file to generate the XML, its already there, so you can use a C:\ location for it no problem.

However, for the PHP script to run and dynamically generate the XML, the PHP needs to run through a web server.

if you just try and open the PHP file from its C:\ location the PHP will not be executed and all you will see is the code itself, not the intended generated xml. This is why its thinking the XML is not well formed, its trying to read the actual PHP code instead of the intended generated XML.

So, when testing locally, the php needs to be run from an absolute location via the localhost:

"http://localhost/testwebsite/xml/myxml.php"

Thats why it worked when you loaded it to your server because even though you were still referencing it relatively, the relative URL was now a http address and the PHP file would be processed propperly by the webserver you uploaded it to.

Hope this helps.

It gave me such a headache trying to figure it out..

thought id share what id found as there wasnt a lot of help about this error on the web.