Save filetree to xml nodetree (Flash Builder 4.5)

I want to have an air application, where I can take a folder structure and copy that structure to a xml. Ultimately I want to save it as a plist for further usage.

My problem is that I don’t get the xml-nodes to nest properly.
For a small amount of folders, all is ok, but with increased size, the folderstructure renders flat.

The problem is that I don’t seem to get the correct reference to the recursive function? I try to preserve the reference, but I must mess something up?

Sorry for the hefty length of code, I’m tired and not creative for the moment.

FLEX CODE

[FONT=Monaco]<?xml version="1.0" encoding="utf-8"?>[/FONT][COLOR=#a7000c][COLOR=#3900fc]<s:WindowedApplication[/COLOR][COLOR=#000000] xmlns:fx="[/COLOR]http://ns.adobe.com/mxml/2009[COLOR=#000000]"[/COLOR][/COLOR]
[COLOR=#a7000c][COLOR=#000000]                       xmlns:s="[/COLOR]library://ns.adobe.com/flex/spark[COLOR=#000000]"[/COLOR][/COLOR]
[COLOR=#a7000c][COLOR=#000000]                       xmlns:mx="[/COLOR]library://ns.adobe.com/flex/mx[COLOR=#000000]"[/COLOR][/COLOR]
                       width="[COLOR=#a7000c]800[/COLOR]" height="[COLOR=#a7000c]800[/COLOR]" backgroundColor="[COLOR=#a7000c]#CACACA[/COLOR]" creationComplete="init()"[COLOR=#3900fc]>[/COLOR]
[COLOR=#037943]<fx:Declarations>[/COLOR]
[COLOR=#900009]<!-- Place non-visual elements (e.g., services, value objects) here -->[/COLOR]
[COLOR=#037943]</fx:Declarations>[/COLOR]
[COLOR=#037943]<fx:Script>[/COLOR]
        <=!=[=C=D=A=T=A=[
            [COLOR=#3941fc]import[/COLOR] mx.events.ListEvent;
            
[COLOR=#00aa12]//fileobjects etc[/COLOR]
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] directory:Array;
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] fileObj:File;
            
            [COLOR=#00aa12]//[/COLOR]
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] indirBool:Boolean = [COLOR=#3941fc]false[/COLOR];
            
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] count:Number = 0;
            
[COLOR=#00aa12]//xml[/COLOR]
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] exportXML:XML;
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] plistxml:XML;
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] tempXML:XML;
                        
                    
            [COLOR=#3941fc]public[/COLOR] [COLOR=#44a97a]function[/COLOR] createplist(fileObj:File, plistxml:XMLList):[COLOR=#3941fc]void[/COLOR]
            {
                directory = fileObj.getDirectoryListing();
                
                [COLOR=#3941fc]for each[/COLOR] ([COLOR=#7ea9d4]var[/COLOR] f:File [COLOR=#3941fc]in[/COLOR] directory)
                {
                    [COLOR=#3941fc]if[/COLOR] (f.isDirectory)
                    {
[COLOR=#00aa12]//if "." skip this folder (mac uses compressed folders as files sometimes, not interested in those)[/COLOR]
                        [COLOR=#3941fc]if[/COLOR](f.name.split([COLOR=#a7000c]"."[/COLOR]).length == 1)
                        { 
                            tempXML = 
                                [COLOR=#3941fc]<dict>[/COLOR]
[COLOR=#3941fc]                                    <key>[COLOR=#000000]{String(f.name)}[/COLOR]</key>[/COLOR]
[COLOR=#3941fc]                                    <array>[/COLOR]
[COLOR=#3941fc]                                    </array>[/COLOR]
[COLOR=#3941fc]                                </dict>[COLOR=#000000];;[/COLOR][/COLOR]
                            
                            [COLOR=#3941fc]if[/COLOR](plistxml.length() == 1)
                            {
                                indirBool = [COLOR=#3941fc]true[/COLOR];
                                
                                plistxml.appendChild(tempXML);
                                createplist(f,plistxml.dict);
                                
                            }[COLOR=#3941fc]else[/COLOR]{
                                
                                textArea.text += [COLOR=#a7000c]" "[/COLOR] + plistxml.length() +[COLOR=#a7000c]"-exceeded length 
"[/COLOR];
                                
                                [COLOR=#7ea9d4]var[/COLOR] savethisref:XMLList = XMLList(plistxml.toXMLString());
                                
                                [COLOR=#7ea9d4]var[/COLOR] parentNode:XML = XML(plistxml.parent());
                                parentNode.insertChildAfter(parentNode[parentNode.length()], tempXML);
                                
                                plistxml = XMLList(parentNode);
                                
                                createplist(f,savethisref.dict);
                            }
                        }
                    }
                }
            }
            
            [COLOR=#3941fc]public[/COLOR] [COLOR=#44a97a]function[/COLOR] createXML():[COLOR=#3941fc]void[/COLOR]
            {                
                exportXML =
[COLOR=#3941fc]<plist version="1.0">[/COLOR]
[COLOR=#3941fc]                        <dict>[/COLOR]
[COLOR=#3941fc]                        </dict>[/COLOR]
[COLOR=#3941fc]                    </plist>[COLOR=#000000];;[/COLOR][/COLOR]
                
                        
                plistxml =
[COLOR=#3941fc]<dict>[/COLOR]
[COLOR=#3941fc]                    </dict>[COLOR=#000000];;[/COLOR][/COLOR]
            }
            
            [COLOR=#3941fc]protected[/COLOR] [COLOR=#44a97a]function[/COLOR] fileList_changeHandler(event:ListEvent):[COLOR=#3941fc]void[/COLOR]
            {
                fileObj = [COLOR=#3941fc]new[/COLOR] File(fileList.selectedPath); 
                [COLOR=#3941fc]if[/COLOR](fileObj.isDirectory){fileTree.directory = fileObj;}
            }
            
            [COLOR=#3941fc]protected[/COLOR] [COLOR=#44a97a]function[/COLOR] exportPlist_clickHandler(event:MouseEvent):[COLOR=#3941fc]void[/COLOR]
            {
                createXML();
                createplist(fileObj,XMLList(plistxml));
                
                exportXML.dict.appendChild(plistxml);
                
[COLOR=#a7000c][COLOR=#7ea9d4]var[/COLOR][COLOR=#000000] data:String = [/COLOR]'<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'[COLOR=#000000];[/COLOR][/COLOR]
                data +=  exportXML.toXMLString();
                [COLOR=#00aa12]//[/COLOR]
                [COLOR=#7ea9d4]var[/COLOR] fileReference:FileReference = [COLOR=#3941fc]new[/COLOR] FileReference();
                fileReference.save(data,[COLOR=#a7000c]"undefined.plist"[/COLOR]);
            }
            
        ]=]=>
[COLOR=#037943]</fx:Script>[/COLOR]
    [COLOR=#3900fc]<s:Button[/COLOR] x="[COLOR=#a7000c]27[/COLOR]" y="[COLOR=#a7000c]18[/COLOR]" label="[COLOR=#a7000c]Up[/COLOR]" click="fileList.navigateUp();"
               enabled="[COLOR=#a7000c]{[/COLOR]fileList.canNavigateUp[COLOR=#a7000c]}[/COLOR]"[COLOR=#3900fc]/>[/COLOR]
    [COLOR=#3900fc]<mx:FileSystemList[/COLOR] id="[COLOR=#a7000c]fileList[/COLOR]" x="[COLOR=#a7000c]27[/COLOR]" y="[COLOR=#a7000c]47[/COLOR]" width="[COLOR=#a7000c]207[/COLOR]" height="[COLOR=#a7000c]248[/COLOR]"
                       change="fileList_changeHandler(event)" directory="[COLOR=#a7000c]{[/COLOR]File.desktopDirectory[COLOR=#a7000c]}[/COLOR]"
                       showExtensions="[COLOR=#a7000c]true[/COLOR]" showHidden="[COLOR=#a7000c]false[/COLOR]" showIcons="[COLOR=#a7000c]true[/COLOR]"[COLOR=#3900fc]/>[/COLOR]
    [COLOR=#3900fc]<mx:FileSystemTree[/COLOR] id="[COLOR=#a7000c]fileTree[/COLOR]" x="[COLOR=#a7000c]242[/COLOR]" y="[COLOR=#a7000c]47[/COLOR]" width="[COLOR=#a7000c]548[/COLOR]" height="[COLOR=#a7000c]248[/COLOR]"[COLOR=#3900fc]/>[/COLOR]
    [COLOR=#3900fc]<s:Button[/COLOR] id="[COLOR=#a7000c]exportPlist[/COLOR]" x="[COLOR=#a7000c]632[/COLOR]" y="[COLOR=#a7000c]366[/COLOR]" width="[COLOR=#a7000c]156[/COLOR]" height="[COLOR=#a7000c]32[/COLOR]" label="[COLOR=#a7000c]Save fileList to Plist[/COLOR]"
              click="exportPlist_clickHandler(event)"[COLOR=#3900fc]/>[/COLOR]
    [COLOR=#3900fc]<s:TextArea[/COLOR] id="[COLOR=#a7000c]textArea[/COLOR]" x="[COLOR=#a7000c]44[/COLOR]" y="[COLOR=#a7000c]325[/COLOR]" width="[COLOR=#a7000c]563[/COLOR]" height="[COLOR=#a7000c]209[/COLOR]"[COLOR=#3900fc]/>[/COLOR]
[COLOR=#3900fc]</s:WindowedApplication>[/COLOR]



just the AS code

[FONT=Monaco][COLOR=#3941fc]import[/COLOR] mx.events.ListEvent;[/FONT]            
[COLOR=#00aa12]//fileobjects etc[/COLOR]
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] directory:Array;
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] fileObj:File;
            
            [COLOR=#00aa12]//[/COLOR]
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] indirBool:Boolean = [COLOR=#3941fc]false[/COLOR];
            
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] count:Number = 0;
            
[COLOR=#00aa12]//xml[/COLOR]
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] exportXML:XML;
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] plistxml:XML;
            [COLOR=#3941fc]public[/COLOR] [COLOR=#7ea9d4]var[/COLOR] tempXML:XML;
                        
                    
            [COLOR=#3941fc]public[/COLOR] [COLOR=#44a97a]function[/COLOR] createplist(fileObj:File, plistxml:XMLList):[COLOR=#3941fc]void[/COLOR]
            {
                directory = fileObj.getDirectoryListing();
                
                [COLOR=#3941fc]for each[/COLOR] ([COLOR=#7ea9d4]var[/COLOR] f:File [COLOR=#3941fc]in[/COLOR] directory)
                {
                    [COLOR=#3941fc]if[/COLOR] (f.isDirectory)
                    {
[COLOR=#00aa12]//if "." skip this folder (mac uses compressed folders as files sometimes, not interested in those)[/COLOR]
                        [COLOR=#3941fc]if[/COLOR](f.name.split([COLOR=#a7000c]"."[/COLOR]).length == 1)
                        { 
                            tempXML = 
                                [COLOR=#3941fc]<dict>[/COLOR]
[COLOR=#3941fc]                                    <key>[COLOR=#000000]{String(f.name)}[/COLOR]</key>[/COLOR]
[COLOR=#3941fc]                                    <array>[/COLOR]
[COLOR=#3941fc]                                    </array>[/COLOR]
[COLOR=#3941fc]                                </dict>[COLOR=#000000];;[/COLOR][/COLOR]
                            
                            [COLOR=#3941fc]if[/COLOR](plistxml.length() == 1)
                            {
                                indirBool = [COLOR=#3941fc]true[/COLOR];
                                
                                plistxml.appendChild(tempXML);
                                createplist(f,plistxml.dict);
                                
                            }[COLOR=#3941fc]else[/COLOR]{
                                
                                textArea.text += [COLOR=#a7000c]" "[/COLOR] + plistxml.length() +[COLOR=#a7000c]"-exceeded length 
"[/COLOR];
                                
                                [COLOR=#7ea9d4]var[/COLOR] savethisref:XMLList = XMLList(plistxml.toXMLString());
                                
                                [COLOR=#7ea9d4]var[/COLOR] parentNode:XML = XML(plistxml.parent());
                                parentNode.insertChildAfter(parentNode[parentNode.length()], tempXML);
                                
                                plistxml = XMLList(parentNode);
                                
                                createplist(f,savethisref.dict);
                            }
                        }
                    }
                }
            }
            
            [COLOR=#3941fc]public[/COLOR] [COLOR=#44a97a]function[/COLOR] createXML():[COLOR=#3941fc]void[/COLOR]
            {                
                exportXML =
[COLOR=#3941fc]<plist version="1.0">[/COLOR]
[COLOR=#3941fc]                        <dict>[/COLOR]
[COLOR=#3941fc]                        </dict>[/COLOR]
[COLOR=#3941fc]                    </plist>[COLOR=#000000];;[/COLOR][/COLOR]
                
                        
                plistxml =
[COLOR=#3941fc]<dict>[/COLOR]
[COLOR=#3941fc]                    </dict>[COLOR=#000000];;[/COLOR][/COLOR]
            }
            
            [COLOR=#3941fc]protected[/COLOR] [COLOR=#44a97a]function[/COLOR] fileList_changeHandler(event:ListEvent):[COLOR=#3941fc]void[/COLOR]
            {
                fileObj = [COLOR=#3941fc]new[/COLOR] File(fileList.selectedPath); 
                [COLOR=#3941fc]if[/COLOR](fileObj.isDirectory){fileTree.directory = fileObj;}
            }
            
            [COLOR=#3941fc]protected[/COLOR] [COLOR=#44a97a]function[/COLOR] exportPlist_clickHandler(event:MouseEvent):[COLOR=#3941fc]void[/COLOR]
            {
                createXML();
                createplist(fileObj,XMLList(plistxml));
                
                exportXML.dict.appendChild(plistxml);
                
[COLOR=#a7000c][COLOR=#7ea9d4]var[/COLOR][COLOR=#000000] data:String = [/COLOR]'<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'[COLOR=#000000];[/COLOR][/COLOR]
                data +=  exportXML.toXMLString();
                [COLOR=#00aa12]//[/COLOR]
                [COLOR=#7ea9d4]var[/COLOR] fileReference:FileReference = [COLOR=#3941fc]new[/COLOR] FileReference();
                fileReference.save(data,[COLOR=#a7000c]"undefined.plist"[/COLOR]);
[FONT=Monaco]            }[/FONT]