Introduction to XML in Flash
       by senocular

XML vs Variable Strings
Ok, so XML can get much more complicated than just text, but for now, that's probably all you need to think of it as being. As such, the big comparison you can make with XML is to urlencoded text which you can load into Flash to obtain external text variables; this via the LoadVars object (or using loadVariables). The XML object in Flash, the ActionScript construct used to load and manage XML, is very similar to the LoadVars object used for urlencoded text variables. Lets take a look at an example of urlencoded text so that we can make a quick comparison between it and XML:

subject=Flash in the News&date=07.25.04&body=Macromedia just announced their support for flashers around the world, those with overcoats and those without.&

The above shows a urlencoded string that would be contained in a text file - a text file which can be loaded into and utilized by Flash. When this happens (via a LoadVars instance), you get 3 variables: subject, date, and body with their respectively assigned strings as values. It's pretty simple and straight forward. You have your basic variable string defined with equal signs (=) and separated with ampersands (&). Not too hard to follow, right? A respective XML file may look like following:

<news>
<entry date="07.25.04">
<subject>Flash in the News</subject>
<body>
Macromedia just announced their support for flashers around the world; those with overcoats and those without.
</body>
</entry>
</news>

The above consists of the same content as the variable string before. Only this time, as XML, it's just laid out a little differently and is organized using tags (XML elements). This, as you can probably tell (color coding aside), makes it a little easier to follow compared to the variable string. It may be a little longer, and thereby requiring slightly more hard drive space to store and making for slightly more bandwidth consumption, but the added clarity is definitely an improvement over the previous format.

When this is brought into Flash, however, you get more than just three simple variables like those which you get with the variable string. Instead, you have a complex data object (contained within an instance of the XML object) harboring not only the content but also the structure of the XML and the hierarchy that defines it. And that is where XML gets scary and confusing. Because it's a markup language, you get that added structure intertwined in with your data.

Why deal with all that mess when you can have three simple variables? Given the example above, it would certainly be easier to use a variable string and LoadVars. Structure and organization may suffer, but that particular instance doesn't really use much of it, right? There are many cases where that is right, where variable strings may be advantageous over XML. These include cases where structure may be irrelevant or where you're dealing with fairly simple, straight-forward data that wouldn't be all that confusing as a variable string.

Structure and clarity are the key elements that XML offers text-based data. Given the circumstances of your situation, you will need to decide whether or not your data requires this or whether you might be better off just using urlencoded variables. You get more speed with the variables but you lose structure and clarity XML provides that structure and clarity but can take longer to load depending on that structure and takes extra effort to parse when interpreted into usable data.


 




SUPPORTERS:

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