This is how I would traverse that XML, seems more natural to me to do it
this way:

Given your XML:

var lobby:XML =
  <lobby>
    <info>
      <id>DE2</id>
      <name>Alex</name>
      <avatar>2_1211369175.jpg</avatar>
    </info>
    <info>
      <id>DE55</id>
      <name>Vasja</name>
      <avatar>55_1232308551.jpg</avatar>
    </info>
  </lobby>;

Then: 

var _people:Array = [];
  
for each (var infoXML:XML in lobby.info)
{
        var personVO:PersonVO = new PersonVO();
        personVO.label = infoXML.name;
        personVO.userid = infoXML.id;
        _people.push(personVO);
}

where PersonVO is a class that acts only as a Value Object (aka DTO -
data transfer object), i.e.:

package
{
        public class PersonVO
        {
                public var name:String;
                public var userid:int;
        }
}

This allows typecasting and avoids generic objects among other things
(like using the Vector class in FP10, you can typecast the array of your
value objects - nice).  See info here on DTOs/VOs:
http://en.wikipedia.org/wiki/Value_Objects 

I would also, if possible, recommend changing your XML to this form
instead:

var lobby:XML = <xml>
                        <items>
                                <item id="DE2" name="Alex"
avatar="2_1211369175.jpg" />
                                <item id="DE55" name="Vasja"
avatar="55_1232308551.jpg" />
                        </items>
                </xml>;
                                
Much more compact that way (6 lines of XML vs. 12), fewer characters,
succinct and as the file grows, will result in a much smaller filesize
(and IMO, also easier to read).  In my opinion, you would only need to
break out into child nodes if the data was large enough to warrant it
(i.e. a paragraph or long sentence of text), or there were sub
hierarchies of the data where it made sense to have child nodes.  In all
other cases, I default to using attributes.  But that's just me.

Hope that helps,

Jason Merrill 

Bank of  America  Global Learning 
Learning & Performance Soluions

Join the Bank of America Flash Platform Community  and visit our
Instructional Technology Design Blog
(note: these are for Bank of America employees only)






-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of
Alexander Farber
Sent: Tuesday, February 02, 2010 5:08 PM
To: Flash Coders List
Subject: [Flashcoders] XMLList question

Hello,

I'm parsing following XML:

var lobby:XML =
  <lobby>
    <info>
      <id>DE2</id>
      <name>Alex</name>
      <avatar>2_1211369175.jpg</avatar>
    </info>
    <info>
      <id>DE55</id>
      <name>Vasja</name>
      <avatar>55_1232308551.jpg</avatar>
    </info>
  </lobby>;

var _people:Array = new Array();

setLobby(lobby.info);

function setLobby(list:XMLList):void {
        for each (var info in list) {
                var item:Object = {label: info.name, userid: info.id};
                _people.push(item);
        }
}

- but when I look in the debugger at the elements
of the _people array, then I see that "label" and
"userid" members are of type XMLList and not String.

And that is why my string comparisions later in the code do not work.

Am I supposed to call toString() explicitly:

                var item:Object = {label: info.name.toString(), userid:
info.id.toString()};

or is there a more natural way?

Thank you
Alex
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to