PDA

View Full Version : [AS3] Load this XML file in to a list component


Gamefreak88
12-13-2008, 04:31 AM
Plain and simple. How do I load this XML file in to a list component?

<?xml version="1.0"?>
<userlist>
<administrators>
<user name="mr-admin" uid="1" />
</administrators>

<moderators>
<user name="some-mod" uid="437" />
</moderators>

<members>
<user name="guest1" uid="5391" />
<user name="guest2" uid="5372" />
<user name="etc" uid="4825" />
</members>
</userlist>

wvxvw
12-13-2008, 09:21 AM
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:XML id="sourceXML">
<userlist>
<administrators>
<user name="mr-admin" uid="1" />
</administrators>
<moderators>
<user name="some-mod" uid="437" />
</moderators>
<members>
<user name="guest1" uid="5391" />
<user name="guest2" uid="5372" />
<user name="etc" uid="4825" />
</members>
</userlist>
</mx:XML>
<mx:Script>
<![CDATA[
import flash.events.Event;

[Bindable]
public var listToDisplay:XMLList;

public function resetList(event:Event):void
{
var reArr:Array = [];
var re:RegExp;
if (administrators.selected) reArr.push("administrators");
if (moderators.selected) reArr.push("moderators");
if (users.selected) reArr.push("members");
if (!reArr.length)
{
listToDisplay = null;
} else {
re = new RegExp("(" + reArr.join("|") + ")");
listToDisplay = sourceXML..user.(re.test(String(parent().name()))) .@name;
}

}

]]>
</mx:Script>
<mx:List dataProvider="{listToDisplay}" width="100%" />
<mx:CheckBox label="administrators" id="administrators" click="resetList(event)" />
<mx:CheckBox label="moderators" id="moderators" click="resetList(event)" />
<mx:CheckBox label="members" id="users" click="resetList(event)" />
</mx:Application>
See if this helps, but, I'd rather use DataGrid for this sort of XML because it may have more than 1 column.
* Notice: you'll need playerglobal.swc version greater than 9.0.115 because for whatever reason RegExp.test() was missing from the previous versions.

Gamefreak88
12-13-2008, 09:39 AM
Thanks for the reply. :)

What in the world is that code? :eek: I've seen it before but only recognize the ActionScript 3.0 and XML parts. The rest.. what is it? lol.

The only thing to be seen is the username. The user id is for if/when I decide to make it clickable to load the users profile OR a right-click menu with "view profile". And the usergroups are technically not shown either.. its just to keep the admins first, mods second and members third. Technically the administrators, moderators and members umm "nodes" I think they're called don't have to be there since my PHP script that outputs the XML puts them in the right order. The only reason I have them there is in case I decide I want to do like (which I'd actually prefer)...

ADMINISTRATORS
- mr-admin

MODERATORS
- some-mod

MEMBERS
- guest1
- guest2
- etc

I need it to be a list component too.

wvxvw
12-13-2008, 10:09 AM
Ah... OK, I see you ware asking about fl.controls.List :)
My code is actually for Flex, meaning, mx.controls.List... I'll see if I can master an example for Flash.

EDIT:
OK, here we go:
import fl.controls.List;
import fl.data.DataProvider;

var xml:XML =
<userlist>
<administrators>
<user name="mr-admin" uid="1" />
</administrators>
<moderators>
<user name="some-mod" uid="437" />
</moderators>
<members>
<user name="guest1" uid="5391" />
<user name="guest2" uid="5372" />
<user name="etc" uid="4825" />
</members>
</userlist>;
var list:List = new List();
var dp:Array = [];
xml..user.(dp.push({ label:@name }));
list.dataProvider = new DataProvider(dp);
addChild(list);
* you need List component in the library to compile this.