Flash based XML parsing

โ€”

by

in

ed note: I am writing this entry because it too me 12 hours or so to cram the solution to my current problem into my head. I am goign to nap. If I lose it all, this entry will give me the fast track on getting it back if I need it ๐Ÿ™‚

Flash and XML, two things that should go together. So for the Client A site I have data in the CMS (content management system) and I need to use it to build submenu’s for navigation. No problem, I used a PHP script to pull it out and build an XML file. Pulling that into Flash is no problem… the thing is, what to do with it?

To start with, I tied it into the nice components that come with Flask 2004 MX. I figure that’s what they are there for. And it works, the XML goes into a component and that gets wired to a list box and blammo… it works. Except the list box looks nothing like I need it to for the client… and the one thing I need to be able to do (make the background transparent) doesn’t seem to be possible.

Ok, backup plan. Put a bunch of label components in the menu area, stack ’em and handle the indexing for the scroll myself. I needed to do it anyway ’cause the scrolling wasn’t going to be standard. So I can’t use the auto XMLConnector, I have to use the regular XML stuff and walk the tree myself.

Example 1

<?xml version=”1.0″ ?>
    <galleries>
        <gallery>
            <title>Art Work</title>
            <id>2</id>
            <weight>25</weight>
            <url>/content/sections/gallery/gallery.php?gallery_id=2</url>
            <target />
        </gallery>
        <gallery>
            <title>Test gallery</title>
            <id>4</id>
            <weight>25</weight>
            <url>/content/sections/gallery/gallery.php?gallery_id=4</url>
        <target />
    </gallery>
</galleries>

So I spent a LONG time looking for a good tutorial on this stuff, and finally found a great tutorial that made it all nice and clear. Then I foudn out Flash is somewhat faster parsing attributes than it is parsing nodes. Now I tend to write node heavy XML (see the above example) but no problem, I re-wrote my PHP and came up with example 2. Don’t let the node name changes throw you, I also wound up making it more generic to the idea of a “menu” so I can re-use the parsing.

Example 2

<?xml version=”1.0″ ?>
<menu>
    <item title=”Art Work” weight=”25″ url=”/content/sections/gallery/gallery.php?gallery_id=2″ target=”main_content” />
    <item title=”Test gallery” weight=”25″ url=”/content/sections/gallery/gallery.php?gallery_id=4″ target=”main_content” />
</menu>

 So there you have it. Make sure you read the tutorial, but I copied the magic here (no, it doesn’t match the above XML, but it is a good template).

The Rosetta Stone of Flash/XML for me…

menuXml = new XML();
menuXml.ignoreWhite = true;
menuXml.onLoad = function(success) {
    if (success) {
        menuItem = this.firstChild.childNodes;
        for (var i=0; i<menuItem.length; i++) {
            item = _root.attachMovie(“itemClip”, “itemClip” + i, i);
            item._x = 0;
            item._y = 20*i;
            item.itemLabel.text = menuItem[i].attributes.name;
            item.myUrl = menuItem[i].attributes.url;
            item.onRelease = function() {
                getURL(this.myUrl,”_blank”);
            }
        }
    }
}
menuXml.load(“myMenu.xml”);