3. Load data from XML files
XML structures look similar to HTML structures, but with one major difference: XML can build his own tags. This way you can imagine any kind of tag that make sense for you, with the only limit that you need to open and close tags in a well formed structure, including nested tags. As general rules we can say:
- Opened tags should be closed
- Nested tags should be closed before outer tags
In order to agree about XML structures, developers usually use DTD (Document Type Definition) that could be internal (defined in the same document) or external, published somewhere in the net. Here's an example of an XML document with an internal DTD:
<?xml version="1.0" encode="UTF-8"?>
<!DOCTYPE data[
<!ELEMENT title(comments, image)>
<!ATTLIST title name CDATA #REQUIRED>
<!ELEMENT comments (#PCDATA)>
<!ELEMENT image (#PCDATA)>
]>
<data>
<title name="Anastasio">
<comments>This is a nice guy</comments>
<image>an2.jpg</image>
</title>
<title name="Anastasio's car">
<comments>Anastasio loves his car</comments>
<image>an1.jpg</image>
</title>
</data>
Here we have the standard mime-type and version declaration (XML), an internal
DTD and the data itself. DTD declaration are not really necessary if you don't
need to agree with others about XML structure, and usually is preferable an
external DTD. If you use non validating parsers, it only takes care of basic
well formed documents. Validating parsers check also XML documents against a
DTD. Since Flash doesn't use a validating parser, there's no special need of
DTD, and you only need to take care about well formess (open closing tags).
XML structure is an easy way of saving structured data, and a usual resource
in replacing databases, especially if you don't have much data and you only
perform read operations. If you have big data or need to perform insert-update
operations, then XML is not the better choice. A good reason to use XML with
Flash, is that trough the XML Object, Flash have build-in support for managing
XML data. Anyway, you always need to walk trough the structure, looping the
tree in order to read data.
Let's go step by step. First we need to create the XML object and load the document
in it:
myXML = new XML()
myXML.ignoreWhite = true
myXML.load("anastasio.xml")
myXML.ref = this
Ok, here we load the data.xml file. The steps are simply: create an XML object, set his ignoreWhite property to true (if not, blank spaces could make fail the parsing process) and load the XML document. Since is an XML object, once loaded, we can use the XML methods and properties to walk trough the document. Explaining how to parse XML files is outside the scope of this tutorial, but take a look at properties like firstChild or nextSibling in the ActionScript dictionary. Here we use the same layout of the second movie in the previous section, that's holders for two elements (title, comments, images) Note that we use a ref variable to point to current timeline, so when parsing, we can access timeline content. Here's a screenshot. You should have two empty movieclips, I have added Anastasio's face in the first because I like it :)

------------------------------------------------------------------------------------------------------------------------------------------
TIP
When working inside callbacks handlers, like XML.onLoad, the scope change to
the XML object, so trying to access timeline MovieClips or Textfields simply
fails. So when building the Object, use always a reference to current timeline
if you need to access timeline content to fetch data.
myXML = new XML()
myXML.ref = this
------------------------------------------------------------------------------------------------------------------------------------------
And here's our code:
myXML.onLoad = function(succes){
if(succes){
var root = this.firstChild
nodes = root.childNodes
for(var i=0; i<nodes.length; i++) {
this.ref["Title_txt"+i].text = nodes[i].attributes.name
subnodes = nodes[i].childNodes
this.ref["Comments_txt"+i].text = subnodes[0].firstChild.toString()
this.ref["holder_mc"+i].loadMovie(subnodes[1].firstChild.toString())
}
} else trace("Error loading XML document")
}
We parse our XML files looking at the document. If you don't know the exactly structure of your document, then is really difficult to walk trough it. Anyway, usually you know it, and you can simply take a look at the XML structure and build your own parser. Again, the succes value indicates if the data have successfully loaded. The binding part inside the onLoad event is almost the same as the one in the previous section, with the only difference that here we manipulate XML data. Complex XML files with a lot of nested trees could be a nightmare, and usually when I become with this kind of structure, I change to a database if possible. Flash MX XML parser is faster than Flash 5 (and I suppose MX 2004 should be faster also), but big pieces of data could take some time to load, and in this case you need to make a preloader using XML.getBytesLoaded() and XML.getBytesTotal() methods.
So we have seen how to load data from txt files and from XML files, let's move
now to a real database

5 most recent
Flash button as Flex icon
Tree menu
Flash Spell Checker
Flash Remoting Library
MX 2004 Chart/Poll
Articles