If what you are doing is looking for nodes with firstnames then you can do 
something like this: 

Assuming Jason's XML with attributes: 

var peopleList:XML = <people> 
<person firstName="Bob" lastName="Smith"> 
<person firstName="Timmy" lastName="Smith" /> 
<person firstName="Jenny" lastName="Jones" /> 
<person middleName="Beth" lastName="Jones" /> 
</person> 
<person firstName="Tom" lastName="Williams"> 
<person firstName="Sa" lastName="Williams" /> 
</person> 
</people>; 

var firstNames:XMLList = peopleList..*.(hasOwnProperty("@firstName")); 
trace(firstNames); 


The ..* automatically selects all nodes that have a firstName attribute 
regardless of nesting. Thus you could loop over firstNames with no recursion 
and get a list of all firstnames. The list will contain all the nodes witha 
firstName attribue, so you will get one node with Bob as firstName that still 
has 3 children, but you will also get 2 of those children in the firstNames 
list so you would simply ignore the children inside of Bob. For testing 
purposes I purposefully added an extra child node with no firstName to Bob. 

This is the result 
<person firstName="Bob" lastName="Smith"> 
<person firstName="Timmy" lastName="Smith"/> 
<person firstName="Jenny" lastName="Jones"/> 
<person middleName="Beth" lastName="Jones"/> 
</person> 
<person firstName="Timmy" lastName="Smith"/> 
<person firstName="Jenny" lastName="Jones"/> 
<person firstName="Tom" lastName="Williams"> 
<person firstName="Sa" lastName="Williams"/> 
</person> 
<person firstName="Sa" lastName="Williams"/> 

You can do something similar with your structure: 

var peopleList:XML = <allPeople> 
<person> 
<personFirstName>Bob</personFirstName> 
<personLastName>Smith</personLastName> 
<person> 
<personFirstName>Timmy</personFirstName> 
<personLastName>Smith</personLastName> 
</person> 
<person> 
<personFirstName>Jenny</personFirstName> 
<personLastName>Smith</personLastName> 
</person> 
</person> 
<person> 
<personFirstName>Tom</personFirstName> 
<personLastName>Williams</personLastName> 
<person> 
<personFirstName>Sa</personFirstName> 
<personLastName>Williams</personLastName> 
</person> 
</person> 
</allPeople>; 

var firstNames:XMLList = peopleList..*.personFirstName; 
trace(firstNames); 

result: 
<personFirstName> 
Bob 
</personFirstName> 
<personFirstName> 
Timmy 
</personFirstName> 
<personFirstName> 
Jenny 
</personFirstName> 
<personFirstName> 
Tom 
</personFirstName> 
<personFirstName> 
Sa 
</personFirstName> 

Your structure produces a simpler list, however I favor Jason's attribute 
version as it is far more compact, simpler to read, etc. 

Personal preference. 

But if you don't need to do recursion then don't. Saves many headaches. 

Sincerely 
Mark R. Jonkman 




----- Original Message ----- 
From: "Theodore Lehr" <[email protected]> 
To: "Flash Coders List" <[email protected]> 
Sent: Friday, March 19, 2010 4:15:11 PM GMT -05:00 US/Canada Eastern 
Subject: RE: [Flashcoders] Recursive:Part II 

sorry a couple of typos... the function should be: 

function createPeople (_xml:*):void 
{ 
var xmlList:XMLList=_xml.children(); 

for each (var fn_xml in xmlList) { 
createPeople(fn_xml); 
trace(item_xml.personFirstName); 
} 
} 

________________________________________ 
From: [email protected] 
[[email protected]] On Behalf Of Merrill, Jason 
[[email protected]] 
Sent: Friday, March 19, 2010 4:07 PM 
To: Flash Coders List 
Subject: RE: [Flashcoders] Recursive:Part II 

I would write the XML this way instead: 

<people> 
<person firstName'"Bob" lastName="Smith"> 
<person firstName="Timmy" lastName="Smith" /> 
<person firstName="Jenny" lastName="Jones" > 
<person firstName="Sal" lastName="Stephens" /> 
</person> 
</person> 
<person firstName="Tom" lastName="Williams"> 
.etc. 

Also, recursive functions call themselves - and then break when some 
value or condition is reached.... for yours, add to your object, and 
then check to see if there are child nodes in the xml below it, if so, 
call the function again to add more, if not, break out of the function. 
However, this will only get you through the top level nodes and one of 
the top level nodes's sub nodes - not the others - I actually can't 
think of how to get into the OTHER sub-nodes - though I know there are 
people on this list who do. I know what some people do is make the first 
pass on the first level, then the second pass on the second, and so on, 
adding to the object as they go. Don't ask me to send you an example 
though. 

Recursive functions are also quite hard to wrap your head around. :) 

Jason Merrill 

Bank of America Global Learning 
Learning & Performance Solutions 

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 Lehr, 
Theodore 
Sent: Friday, March 19, 2010 3:54 PM 
To: Flash Coders List 
Subject: [Flashcoders] Recursive:Part II 

So I have this xml: 

<allPeople> 
<person> 
<personFirstName>Bob</personFirstName> 
<personLastName>Smith</personLastName> 
<person> 
<personFirstName>Timmy</personFirstName> 
<personLastName>Smith</personLastName> 
</person> 
<person> 
<personFirstName>Jenny</personFirstName> 
<personLastName>Smith</personLastName> 
</person> 
</person> 
<person> 
<personFirstName>Tom</personFirstName> 
<personLastName>Williams</personLastName> 
<person> 
<personFirstName>Sa</personFirstName> 
<personLastName>Williams</personLastName> 
</person> 
</person> 

To start I am just trying to recursively go through this to grab the 
first names. I am trying: 

createPeople(peopleXML) 

function createPeople (_xml:*):void 
{ 
var xmlList:XMLList-_xml.children(); 

for each (var fn_xml in xmlList) { 
createChart(fn_xml); 
trace(item_xml); 
} 
} 


I am wondering - am I on the right track? Does the xml look well-formed 
for doing what I am trying to do? 

Thanks! 
_______________________________________________ 
Flashcoders mailing list 
[email protected] 
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders 
_______________________________________________ 
Flashcoders mailing list 
[email protected] 
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders 
_______________________________________________ 
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