Introduction to XML in Flash
       by senocular

Sending XML Out From Flash
We've already covered loading XML in Flash and editing it while its there, but haven't really touched getting it back out again. This is something you may need to do once you spend all that time editing an XML document for some anxiously awaiting server. Hurry hurry hurry!

Unlike editing XML, sending it out of Flash is not that hard. In fact, it may be easier than loading it (maybe). It involves using one of two XML methods:

Property Represents
XML Instances
send("URL", "target") sends the XML to the passed URL.
sendAndLoad("URL", receiver) sends the XML to the passed URL and performs a load command on the XMLInstance passed loading the result.

Both send and sendAndLoad send your XML to a URL, namely some dynamic page waiting to handle your XML for the server. The sendAndLoad method takes the extra step of loading back in that URL to a target receiver object (typically an XML or LoadVars instance). With the send method you just pretty much have to assume all went to plan, though it does offer a optional target HTML window parameter ("target") to display the results. Valid targets include "_self", "_blank", and HTML frames etc.

Using sendAndLoad is much like calling both the send and load method back to back only here, it gives the URL time to react to the XML sent. That result then ends up into the receiver object and its onData is called (which would then call onLoad if you haven't rewritten it yourself).

[ interaction of external content with a flash movie ]

You can see from the above diagram that Flash really doesn't interact directly with a server here. When using load, send, and sendAndLoad you're interacting with dynamic pages that interact with the server for you. Flash has ActionScript; your dynamic pages use PHP or ASP (or other). The interaction of Flash and the server revolves around text documents passed back and forth between these dynamic pages. Flash really only cares about the resulting text of its request. When you load something like XML into Flash, you're loading text. This text can be generated by a server but there are no direct exchanges of commands between that server or Flash. Even with sendAndLoad, Flash sends its text to a dynamic page which has its own commands to interact with the server that then allows it to generate a new, dynamic page of text that can then be brought back into Flash.

When XML data is sent to a server from Flash using one of these methods, it arrives in the form of raw POST data. This means that it comes as POST data that has no variable associated with it as often is the case POST data. Once your XML reaches your server you'll have to know how to handle that using server-side scripting (the two examples that follow will provide a simple method of doing so for PHP).

One thing to be careful of when using send and sendAndLoad is that both send XML to a URL with a default content type of application/x-www-form-urlencoded. This is the format of urlencoded variables that you deal with in using a LoadVars instance. XML, however, is not urlencoded - its just XML text. If you're working with your server-side script and find that your server isn't liking your XML it may be because of this. Fixing this means changing the content type of your XML instance, and for that you use the contentType property.

var my_xml = new XML();
my_xml.ignoreWhite = true;
my_xml.contentType = "text/xml";
...

The text/xml content type will help your server recognize that it is, in fact, receiving XML and not urlencoded variables and may help solve any problems you've been having. Like with ignoreWhite, if you plan on using send or sendAndLoad, setting the contentType to "text/xml" is pretty standard practice.

But wait! There's more! Another precaution! Wha... Yeah, I know, I made it sound like it could have been something good, but it's not. It's just something else to worry about. This precaution has to deal with working in the Flash authoring environment when using methods like send and sendAndLoad. When working in Flash, when you test your movie, Flash will send sent XML data through GET and not POST as it does through a web page. This can screw you over just as much as the contentType. Your best bet for testing from Flash when dealing with server interaction like this is to perform a publish preview in HTML so you can see the movie working directly in the browser.

Note: XML Declaration Bug
There exists a XML declaration bug where when saving (and reloading) an XML file to the server repeatedly from Flash, Flash can inadvertently create duplicate XML declaration tags to be stacked on to the XML document. To prevent this behavior, you'll just have to do something such as start with a fresh XML instance of clear the XML declaration from the XML instance in use. You will see this dealt with first hand in the examples to come.

 


 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.