views:

152

answers:

2

I need to extract data from a structure and put it into a list, but I don't know how many levels the structure has.

For each level, I can call level.children(), if there are no levels below the current one, it returns [], if there are, it returns [object, object, ...], on each of which I can call children() on again.

I need to drill down through the structure until I've extracted all levels of data into a list.

When based off a structure like this:

<name>John Smith</name>
<team link="http://teamwebsite.com"&gt;
 <name>Team Name</name>
</team>
<games>
 <location>
  <venue>A stadium</venue>
 </location>
</games>

The list should look something like this:

[
 [
  {'name': 'name', 'attrs': {}, 'text': 'John Smith', 'parent': None},
 ],
 [
  {'name': 'team', 'attrs': {'link': 'http://teamwebsite.com'}, 'text': '', 'parent': None},
  {'name': 'name', 'attrs': {}, 'text': 'Team Name', 'parent': 1}, # the reference to its parent's position in the list
 ],
 [
  {'name': 'games', 'attrs': {}, 'text': '', 'parent': None},
  {'name': 'location', 'attrs': {}, 'text': '', 'parent': 1},
  {'name': 'venue', 'attrs': {}, 'text': 'A stadium', 'parent': 2},
 ],
]

I'm trying to figure out the Python I would use to get from the data structure to my list. I need a kind of self-perpetuating for loop, but I can't come up with a good solution.

Anything to point me in the right direction? I'm sure there is some good theory for this kind of thing that I completely don't know about but would be happy to read.

+10  A: 

You're describing recursion, but I'm guessing there are better, ways, to, parse, XML.

Jason Punyon
Is there? XML is a tree. Although if you had an XSD, you would know which nodes could have children and how many (even if "how many" was unbounded).
Thomas Owens
@Thomas Owens: By parsing it I mean not having to write the algorithm to parse it yourself...
Jason Punyon
Ah, a built-in iterator. How wonderful :p. Thanks for those links, I'll have a read of them all. Cheers.
@efcjoe: Always happy to help :)
Jason Punyon
+5  A: 

The concept you're looking to use here is called "Recursion".

Chris McCall
Yep, it was. And I'm going to read about it now. Thanks.