You could use this XML or create your own or read from a database and generate XML, your call. This is what the web service will do.
Case 1
In this case, the web service will return the XML in uncompressed form.
<?php
header("Content-type: text/xml");
$file = 'books.xml';
$xml = file_get_contents($file);
echo $xml;
?>
The idea is to print the XML whenever this PHP is requested.
The idea is to output the XML whenever the web page is requested. We will check the download time from a client as3 code. Host this PHP and the XML file on a web server. For test purposes, I’ve hosted in a WAMP server. You could download one here.
You can find the as3 source code below to fetch data from the webservice.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.Timer;
/**
* ...
* @author Elvis Fernandes
*/
public class Main extends Sprite
{
private var theXML:XML = new XML();
private var theURL:String = "http://localhost/xmlwithoutcompression.php";
private var theRequest:URLRequest;
private var theLoader:URLLoader;
private var startTime:int;
private var endTime:int;
public function Main()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
theRequest = new URLRequest(theURL);
theLoader = new URLLoader();
theLoader.load(theRequest);//load the webservice
startTime = (new Date()).getTime();//storing time the request was made by the client
theLoader.addEventListener(Event.COMPLETE, onXMLLoaded);//fired when the xml is received
}
private function onXMLLoaded(e:Event):void
{
endTime = (new Date()).getTime();//store the time when the xml is received
trace("finished loading took " + (endTime-startTime) + " ms");
//trace(theLoader.data);
}
}
}
Case 2
Now, in this case, the web service will return the XML in compressed form.
<?php
header("Content-type: text/xml");
$file = 'books.xml';
$xml = file_get_contents($file);
$data="<?xml version='1.0'?>";
$data=$data."<data>".base64_encode(gzcompress($xml))."</data>";
print_r($data);
?>
This PHP will generate an XML file with the following output. You can try accessing the URL by hosting the XML and PHP files in localhost.
<?xml version='1.0'?>
<data>eJy1WE1z2zYQvXem/2HHPeQQyZJsJ6kzijK2mjhp7MYTuW1uHYhYihiDAAOAUtRf3wfS+kgsZqg0PtkkPrh47+HtroYvP+ea5uy8subFweCwf/By9PNP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jMeNjgVNVglXxUhuFMh41kN8P1dMdqNZV0TVXpe9uUK+FE9J+RhbwTRXJBGka2qTXKomBNED79EdrtKpMRKykhYRGY76CQlUB0TkLG0aKe8PKs4FZzlZb/IlAMVVF9aI7UNEJfhc5uJgIvUBVIhUgj31Kpd3C2anm3G/XhFOOVfqnEt8r8704GPTvc/D+0TmkAIu4+VZFnjjrbRro8I9XN88pZptrZ2dO5HmM5VwhyO9rbY7b0gLFn7ahZR0rslOMFvSooFDawTWUJ/5caBtzt9rU2ZJBiI5vkHt8bT3F3fGq1s3xnWvvh/aOBrIV2hP0WcfP6Ywido6RY3yM/n80kPug3KqBjArYqKKOmArhfHSH2BXNeSfInbqRRO0RPQZlIYqKqlF+f4XJOEAqkmg7HyeXN5EDPHgobFO2TM4+VkaUg8T92NjRi16g9cfHrtQtN5Hxl/Kl0DD+UipLz34oKyenLavAk5Z96oaRr8L+WvlV57/2+/gLQNX4B+SHxWrtufAq6ayexo8fozr7pW5hzybXj9ELb0rwSN0sdsFxd1AqoIBtkGT9O0geu4PVGjZzhSIyvvsmkcPe6jea/wBisG55
</data>
We can now read this only after decrypting it. A sample as3 code is given below.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.utils.Timer;
import mx.utils.Base64Decoder;
import flash.utils.CompressionAlgorithm;
/**
* ...
* @author Elvis Fernandes
*/
public class Main extends Sprite
{
private var theXML:XML = new XML();
private var theURL:String = "http://localhost/xmlwithcompression.php";
private var theRequest:URLRequest;
private var theLoader:URLLoader;
private var startTime:int;
private var endTime:int;
public function Main()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
theRequest = new URLRequest(theURL);
theLoader = new URLLoader();
theLoader.load(theRequest);//load the webservice
startTime = (new Date()).getTime();//storing time the request was made by the client
theLoader.addEventListener(Event.COMPLETE, onXMLLoaded);//fired when the xml is received
}
private function onXMLLoaded(e:Event):void
{
endTime = (new Date()).getTime();//store the time when the xml is received
trace("finished loading took " + (endTime-startTime) + " ms");
var theXML:XML = new XML(theLoader.data);//retrieve the data
var b64:Base64Decoder = new Base64Decoder();
b64.decode(theXML.toString());
var bArray:ByteArray = b64.toByteArray();
bArray.uncompress(CompressionAlgorithm.ZLIB);
//trace(bArray);
}
}
}
This is it, we are done with the code. Now off to testing how much truth is in it. We need to really evaluate whether compressing XML data actually helps transfer files faster.