4

I am trying to dynamically create an XML file using php like this:

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

  // create doctype
  $dom = new DOMDocument("1.0");

  // create root element
  $root = $dom->createElement("tracklist");
  $dom->appendChild($root);
  $dom->formatOutput=true;

  // create child element
  foreach ($commonPlaylist as $value) {

     $trackArray = getTrackForID($value['ID']);

     $item = $dom->createElement("track");
     $root->appendChild($item);

     foreach ( $trackArray as $key => $value) {
      $attr = $dom->createAttribute($key);
      $item->appendChild($attr);

          $attrValue = $dom->createTextNode($value);
      $attr->appendChild($attrValue);
     }

  }

 echo $dom->saveXML();

The output of the file is normal working xml

<?xml version="1.0"?>
   <tracklist>
        <track ID="4" title="Track01" artist="Artist01" url="" length="" coverURL=""/>
        <track ID="1" title="Track02" artist="Artist02" url="" length="" coverURL=""/>
        <track ID="8" title="Track03" artist="Artist03" url="" length="" coverURL=""/>
   </tracklist>

However if I want to get this data into as3 with the following code:

var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("getPlaylist.php"));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void
{
    var myXml:XML= new XML(e.target.data);
    trace(myXml);  // The variable is being traced.

}

I don't get any output at all. If I read the file as a String I get the whole PHP code. What am I doing wrong?

Thank you in advance for any help.

Regards, Matteo

3 Answers 3

3

If I read the file as a String I get the whole PHP code

It sounds like it's not being executed server-side. Are you hosting it on a server with PHP installed?

Sign up to request clarification or add additional context in comments.

2 Comments

I could so punch myself in the face right now. I am testing this on a local server. I always kept checking the file in my browser like this localhost:8888/Project/getCommonPlaylist.php while I loaded it into the flash file without the localhost:8888 that's why PHP didn't work. Thanks a lot!!!
Jap was correct I just had to wait a few minutes until I could accept. Thanks
2

Check the variable name

  • You're tracing myXML & the XML variable name is myXml

2 Comments

I typed that while writing my question. It's not the problem inside my code.
Alright, corrected the variable name. How about you trace the output from the URL request before creating a new XML object? Does that show the output? correct me if I am wrong. I guess it won't , since the URL should be specify the scheme http:// 'localhost/getPlaylist.php'
0

Is processXML() actually triggered? Try modifying it like in the following code and see if you get the "Hi!" part traced.

function processXML(e:Event):void
{
    trace('Hi!');
    var myXml:XML= new XML(e.target.data);
    trace(myXML);
}

BTW, using a Flash debugger might be much more useful from trying to guess what went wrong :) Since the one shipped with Flash Professional sucks big time I can only recommend DeMonster debugger which saved me a lot of time while working on Flash projects. It also wouldn't hurt to have an HTTP traffic analysis tool like Fiddler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.