Recursive constructing of an xml object in flash
January 24, 2005 10:53 AM   Subscribe

I need help with recursive constructing of an xml object in flash.
Google just doesn't cut it for me - maybe you can [+]

Say i've got a directory of dragable files and folders in flash. And i want to save the structure of it in xml. The folders can contain folders that contain folders and so on.

How do i go about creating the xml that reflects this structure - as far as i understand i have to make some kind of recursive function that places each object in the right place in the xml-object. But I can't get my head around it.

First of all i would like to know if what i want has a name (i know its not a recursive parser - is it a compiler?=
Second helpful code-snippets, code-outlines or tutorials would be very greatly appreciated.

Thank you all very much for the anticipated solution to my problems.
posted by FidelDonson to Computers & Internet (3 answers total)
 
ActionScript does indeed support recursion, but like every other language in the world where recursion is possible, you have to be careful to avoid looping forever.

The best way to think of recursion is a function that calls itself with a smaller set of the problem.

You'd probably want something that's logically like this, (sorry for non-functioning code, I'm in a hurry and my actionscript is lacking)

function parse_directory (xml_structure, directory_name)
{
open (directory_name)

for each file in directory
{
add file to xml_structure

if (file is a directory)
parse_directory (xml_structure, file)
}
}

The above will recursively go through your directories, and sub directories, adding them to your XML.
posted by robinw at 2:05 PM on January 24, 2005


Response by poster: Thank you - i kind of figured that part of it out by myself after posting. The part that i can't figure out is how to place the xml-node in the correct place in the xml-tree and do it dynamically.

say i have a folder with a folder with a folder. When i have parsed the third level folder into an xml-node how do i place that node as a child of the second level node?

And even worse how would i go about placing a new second level folder after the third level folder?
posted by FidelDonson at 3:14 AM on January 25, 2005


The trick is not to have each folder add itself to its parent folder, but to have the parent folder add each child folder to itself. You can do this by having a parse_directory function that returns an XML node for the directory it's supposed to parse.

function parseDirectory(directory)
{
    var node = document.createElement("directory");

    for each file in directory
    {
         if (file is File)
             node.appendChild(parseFile(file));
         else if (file is Directory)
             node.appendChild(parseDirectory(file));
    }

    return node;
}

See, the parseDirectory() function always returns a <directory/> element for the directory it parsed. Presumably, the parseFile() will correspondingly return a <file/> element.
posted by Khalad at 6:24 AM on January 25, 2005


« Older What is the origin of the phrase "patience...   |   Isn't it possible to print a story uninterrupted... Newer »
This thread is closed to new comments.