...

View Full Version : Programatically create XMLListCollection???



loki421
04-01-2010, 03:28 AM
Hi guys,

I've been trying to figure this one out for a while and can't seem to get my head round it. I'm trying to build a dynamic tree from a cfdirectory query, I've looked all over but can't find a good solution, so I'm giving it a go in AS3.

I've got the arraycollection that's returned from the server via RemoteObject.

What i want to do is loop over this and select only objects with certain properties, and build my XMLListCollection in AS3 before i send it to the tree.

So, i have the initial loop of the array collection where i want to extract the directories only, pop them into the XMLListCollection as top level nodes, then loop back through the same thing over and over until i rebuild the heirachy data that the root folder looks like, then finally send it to the tree (fingers crossed!) :p

So, here's what i got so far:


private function rhServer(event:ResultEvent):void{
treeAC = event.result as ArrayCollection;
var treeXML:XMLListCollection = new XMLListCollection;
var AC:ArrayCollection = new ArrayCollection;
var i:int
for (i = 0; i < treeAC.length; i ++){
if(treeAC.getItemAt(i).Type == "Dir" ){
AC.addItem(treeAC.getItemAt(i).Name);
trace (treeAC.getItemAt(i).Name);
}
}
}


This lets me populate at AC ArrayCollection with the four folders that happen to be in the root folder, but i've tried the same method with addItem on the XMLListCollection and i keep getting errors, it just wont let me construct an XMLListCollection from scratch.... where am i going wrong?

Here's the code that returns the error:


private function rhServer(event:ResultEvent):void{
treeAC = event.result as ArrayCollection;
var treeXML:XMLListCollection = new XMLListCollection;
var i:int
for (i = 0; i < treeAC.length; i ++){
if(treeAC.getItemAt(i).Type == "Dir" ){
treeXML.addItem(treeAC.getItemAt(i).Name);
trace (treeAC.getItemAt(i).Name);
}
}
}


So what i'm aiming for here is to create an XMLListCollection that'll read something like:

parent folder
.....chid folder
..........child file
.....child file
parent folder
.....child file
.....child file
parent folder
.....child folder
..........child file
file
file
file


etc etc.

So the question is can i build an XMLListCollection from scratch programatically?

Any help will be very gratefully received!

Many thanks in advance all :D

loki421
04-01-2010, 07:37 PM
ok, so i'm getting there slowly....

The problem i have now is that when i create the XML with this code it adds a child with the same value to the folder (node? branch? not sure :p)



private function rhServer(event:ResultEvent):void{
treeAC = event.result as ArrayCollection;
var treeXML:XMLListCollection = new XMLListCollection;
var i:uint
for (i = 0; i < treeAC.length; i ++){
if(treeAC.getItemAt(i).Type == "Dir" ){
var d:XML = <folder/>; //called var 'd' to remind me it's a directory, so should have childern eventually...
d.appendChild(treeAC.getItemAt(i).Name);
treeXML.addItem(d);
trace (d.toXMLString());
trace ("Trace name of directory = " + treeAC.getItemAt(i).Name);
}
}
directoryTree.dataProvider = treeXML;
}


so this gives me a result like:

parent folder
.....parent folder copy (but as a child of folder)
parent folder
.....parent folder copy (but as a child of folder)
parent folder
.....parent folder copy (but as a child of folder)

Does that make sense? It's creating a folder correctly, but then places a child/file inside each folder and replicates it's name....

Does anyone know what's going wrong here???

Thanks! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum