May 15, 2003
xml2swf: less fat - full flavour

When it comes to XML I must admit that I was not what you would call an "early adopter". The reason is that XML is so bloated. If you look at its data-to-structure ratio XML is not really the most efficient way to transport information.

But of course there are so many things that speak in favour of XML that finally it has also become my favourite data transport vehicle. But the filesize is still something that bothers me.

That's why I wrote a server-side PHP script which can reduce XML filesizes a lot without changing the way that you access the data. It achieves this by creating a compressed swf out of the xml file. Instead of loadXML you use loadMovie to get the data. After the loading has finished you'll find an XML-object with a name of your choice inside. And instead of 32500 bytes it's merely 7400 bytes small.

Download xml2swf here. Use is free for non-commercial purposes. If you want to implement it in a commercial product, I ask for a donation of US$10,- or €10,- via PayPal. Honesty prevails!

Posted at May 15, 2003 06:44 PM | Further reading

I'm gonna play with this and send you some cash soon. Sounds great and much needed. I am building a multinode network heavily based on XML much like cyberfunk's FML

Posted by: Leadbased on May 15, 2003 07:49 PM

I should add that of course the compression ratio get's better the bigger the original XML file is. If you have just a 300 byte source file the overall profit will not be as much.

Posted by: coma2mario on May 15, 2003 07:56 PM

it is very interesting that many people's only concern about XML is that it is a verbose format to structure data.
it is even more interesting that usually it turns out that those people either don't understand the concepts behind XML (ie. lack the big picture) or blame XML because a technology like Flash seems to have problems handling it. it's like trying to drive a porsche offroad and blaming nature afterwards.

Posted by: Claus Wahlers on May 15, 2003 08:25 PM

very nice utility....


Posted by: Nik Khilnani on May 15, 2003 10:05 PM

This sounds cool.

One thing to be aware of is that Flash triggers an xml.onLoad event when a normal XML file object is fully loaded...

This isn't always true of a movieClip. Many people complain about the fact that a movieClip doesn't have a "finished loading" event, because the movieclip.onLoad handler fires when the movieClip first appears *on the stage*, not necessarily when it's finished loading the data.

So it would seem necessary to write additional code in order to make sure that the XML movieclip is fully loaded before processing it...

Posted by: andy makely on May 16, 2003 08:48 PM

This is really cool! Great idea. Congrats.

Posted by: Burak KALAYCI on May 18, 2003 12:28 AM

Andy: you are absolutely right and I'm thinking about adding an optional onLoad callback in v1.1. Currently you will have to use the usual getBytesLoaded vs. getBytesTotal check.

Posted by: coma2mario on May 19, 2003 09:14 AM

wazz up

Posted by: imran on May 30, 2003 01:25 AM

I like the concept, especially for offline browsing. But if you only need to use the .swf in a browser the following is sufficient.

Create a php file, in this case I call it 'xmlreader.php', with the following code:

if(!empty($_GET['FileName']) && file_exists($_GET['FileName']))
$FilePointer = fopen( $_GET['FileName'], "r" );
$FileContents = gzencode( fread($FilePointer, filesize($_GET['FileName']) ) );

header("Content-Encoding: gzip");
header("Content-Length: " . strlen($FileContents));

echo $FileContents;

Then in your .fla call the xml as usual, trough the xmlreader.php file

This method will only work in a browser and not trough the flash player! And remember this example is not secured in any way, people can supply any FileName and it will be returned!

Posted by: sheeponfire on June 4, 2003 02:09 PM

Very interesting idea.

I noticed in the PHP code that you use flash 6 headers. Do you have a flash 5 version?


Posted by: Morgan on October 22, 2003 03:52 PM

Morgan: theoretically that is possible, but it's not practical. The idea of this is to utilize the gzip compression of flash 6 to reduce the size of the xml. With flash 5 that's not possible.

Posted by: Mario Klingemann on October 22, 2003 03:56 PM


I have downloaded the xml2swf from the link above. But to test the code basically I have no idea about...

1. what would be the input XML file (DTD) format
2. will the code creates a SWF file on disk or we need to have a SWF file

Please let me know

The bottom line is I need a little extra documentation about how exactly I can put this to use. Any XML or other reuired file samples are greatly appreaciated.

If it works for me you have 30 USD the next day.


Mohammed Ali

Posted by: Mohammed Ali on December 9, 2003 12:11 PM


It's me again.

I tried the XML2SWF files downloaded and it did not do anything.

I have used the sample xml2swf-test.php as follows...

I have changed the second parameter name to "swfout" 'coz that is waht the sample program is trying to capture.

except the above one change I did not alter the code.

My system is with PHP 4.3.2 installed over Windows 2000 + IIS.

lease let me know what are all th other resources that I have to configure/install in order to get the application up and running.

And as requested earlier, is there a specific format of XML which the XML2SWF accept.


Mohammed Ali

Posted by: Mohammed Ali on December 9, 2003 01:46 PM

If you want to write a file back to the folder you have to take care that PHP has got the write permission for that folder. You will have to chmod that folder.

I normally do not write static files but directly return the swf with the request.


Posted by: Mario Klingemann on December 9, 2003 01:52 PM

Hi Mario

Thanks for the comment. I have created a flash file and used the ActionScript command as you have given above. But it is not working. the write permissions are there for the folder.

Please let me know is there a specific format of XML to be used. I had an XML that is generated by JavaSWF (the Home.xml).

Please let me know. I am relly thankful for your help


Mohammed Ali

Posted by: Mohammed Ali on December 9, 2003 04:06 PM

Hello Mario

Please let me know the XML format to be used with the xml2swf code.

And also the other references required to proper running of the code on a windows system.


Posted by: Mohammed Ali on December 10, 2003 12:46 PM

Any well-formed XML should do.

You can test your document in actionscript:

var test=new XML();

where "..." is your document. if the trace returns 0 it should also work with xml2swf. Perhaps your document is too long - maximum allowed size is ~64K.

Posted by: Mario Klingemann on December 10, 2003 01:49 PM

Hi, developers!

We have released a toolkit for Flash generation from XML files. You can see examples at Free version also is available for download.

Posted by: Roman Petrov on December 9, 2004 02:48 PM

Look I know xml is bloated, but I find just gzipping the data by hand or by cgi, delivering the compressed xml with Content-Encoding: gzip header works fine, the browser inflates and parses it to flash, no headache.

Posted by: testicide on April 11, 2005 05:11 PM

That is amazing stuff! I congratulate you on this development and thank you for sharing it with the world.
Now I am just wondering how large the load on a php server will be when going for a lot of xmls, and whether the time php takes to compress the xml outweighs the gain in faster loading. What do you think ?

Posted by: Thilo on August 28, 2005 05:11 AM

Why not just use AMFPHP - - it works really well, and does all the serializing in both ends. It uses flashs internal Actionscript Message Format which is really fast and even more compressed than this solution (puts less of a stress on the server and is way faster for flash to parse too).

XML is nice, but when it comes for fast or large transfers, its not worth the effort.

Oh, did I mention that AMFPHP is opensource :)


Posted by: Mads Sülau Jørgensen on November 18, 2005 01:48 PM
Post a comment

Email Address:



Remember info?

Thank you!

Most Visited Entries
Sketches, Works & Source Code
In Love with
Powered by
Movable Type 2.661

© Copyright Mario Klingemann

Syndicate this site:
RSS 1.0 - RSS 2.0

Quasimondo @ flickr
Quasimondo @ LinkedIn
Quasimondo @ Twitter
Quasimondo @ Facebook
Quasimondo @ MySpace
Quasimondo is a Bright
Citizen of the TRansnational Republic
My other blog in german

My family name is written Klingemann,
not Klingelmann, Klingeman, Klingaman, Kingemann,
Kindermann, Killingaman, Klingman, Klingmann, Klingonman
Klingemman, Cleangerman, Klingerman or Kleangerman

profile for Quasimondo at Stack Overflow, Q&A for professional and enthusiast programmers