Tutorial 2: Managing XML/SWF payloads easily

by Tom Byrne 13. May 2010 08:48

This is part two of a three part tutorial.

  1. Using Multiple XML sources
  2. Loading classes from other SWFs
  3. Download Source

1. Using Multiple XML sources

If your app is big enough, you won’t want your users to download all XML content when it starts, you’ll want to break down content into chucks which are loaded only when the app needs them. This is easy in SiteStream by linking your XML sources together.

This example expands on the example in Part 1 by including a second XML file (myXML2.xml), this is done by adding a ‘url’ attribute to the requested item (root/item2 in myXML.xml), which is recognised by SiteStream as a special attribute. This second XML file contains the fully populated version of the object (as opposed to the empty ‘stub’ version in the myXML.xml file). Notice that the code hasn’t changed at all, SiteStream will realise that the requested item references another XML source and will automatically load in the additional content. This would also be the case if the code requested any object deeper within the myXML2.xml file (i.e. “root/item2/childObject”).

 

myXML.xml:

<Array id="root"
myPackage="myPackage.*">
<myPackage:MyClass id="item1"/>
<myPackage:MyClass id="item2" url="myXML2.xml"/>
<myPackage:MyClass id="item3"/>
<myPackage:MyClass id="item4"/>
<myPackage:MyClass id="item5"/>
</Array>


myXML2.xml:

<myPackage:MyClass id="item2" colour="0xff0000"
myPackage="myPackage.*">
<myPackage:MyClass id="childObject"/>
</myPackage:MyClass>

 

SiteStreamTest.as:

package
{
import org.farmcode.siteStream.SiteStream;
import flash.display.Sprite;
import myPackage.MyClass;
public class SiteStreamTest extends Sprite
{
public function SiteStreamTest(){
var includeClass:Class = MyClass;
var siteStream:SiteStream = new SiteStream();
siteStream.rootURL = "myXML.xml";
siteStream.getObject("root/item2",onSuccess);
}
private function onSuccess(e:MyClass):void{
trace("Item 2 retrieved");
}
}
}

 

The SiteStream object has a property 'baseDataURL', which allows the setting of base URL which will be prepended to the beginning of the XML URLs included in your data. This property helps with multiple environments where the relative path to XML files might change. This value is not used for the root XML load.

2. Loading classes from other SWFs

When you’re app becomes large, you may wish to break down the SWF into several parts (‘Modules’ in Flash Builder), generally the issue with this is loading these SWFs in at the right time. SiteStream makes this very easy by allowing SWFs to be referenced in the package/namespace declarations of the root node.

Note, the only change I have made to SiteStreamTest.as is removing the reference to MyClass (to avoid MyClass being included in the main SWF).

The following example builds on example in Part 1, notice the MyClass Class has been moved from the SiteStreamTest SWF into the CodeLibrary SWF, which is then referenced in the XML package declaration.

myXML.xml:

<Array id="root"
myPackage="CodeLibrary.swf::myPackage.*">
<myPackage:MyClass id="item1"/>
<myPackage:MyClass id="item2"/>
<myPackage:MyClass id="item3"/>
<myPackage:MyClass id="item4"/>
<myPackage:MyClass id="item5"/>
</Array>

SiteStreamTest.as:

package
{
import org.farmcode.siteStream.SiteStream;
import flash.display.Sprite;
public class SiteStreamTest extends Sprite
{
public function SiteStreamTest(){
var siteStream:SiteStream = new SiteStream();
siteStream.rootURL = "myXML.xml";
siteStream.getObject("root/item2",onSuccess);
}
// Removed reference to MyClass to avoid inclusion
private function onSuccess(e:*):void{
trace("Item 2 retrieved");
}
}
}

 

CodeLibrary.as:

package
{
import myPackage.MyClass;
public class CodeLibrary extends Sprite
{
public function CodeLibrary(){
var includeClass:Class = MyClass;
}
}
}

The SiteStream object has a property 'baseClassURL', which allows the setting of base URL which will be prepended to the beginning of the SWF URLs included in your data. This property helps with multiple environments where the relative path to SWF files might change.

 

3. Download Source

 

< Goto Tutorial 1 | Goto Tutorial 3 >

Tags:
Categories:

Comments

5/13/2010 10:19:43 AM #

Tutorial 3: Node Referencing

Tutorial 3: Node Referencing

FARMCode.org

12/9/2010 8:58:08 PM #

hi,
could you please show an example how to put in an xml more than one custom class? I tried to add MyClass2 to the myXML.xml and evety time i'm trying to request it i got an error message: like coudnt find the class.

edited:
i've found magic code:
var includeClass:Class =MyClass2;

with that it start working... any advise? thx

Regards,
Leonid

Leonid Ukraine