[Tutor] small ElementTree problem

Alex Hall mehgcap at gmail.com
Thu Jan 27 23:23:15 CET 2011


Hi all,
I am using, and very much enjoying, the ElementTree library. However,
I have hit a problem. Say I have something along the lines of:

<service>
<message>Message from Service</message>
<version>1.0</version>
 <list>
  <result>
   <id>1</id>
   <name>result 1</name>
  </result>
  <result>
   <id>2</id>
   <name>result 2</name>
  </result>
 </list>
</service>

In my ResultSet class, I parse this to get the text of elements like
message or version. Then, I use root.findall("list/result") and
iterate over the result, passing to a second function which parses the
passed-in elements into Result objects. For example:

all=root.findall("list/result")
for i in all:
 self.mylist.append(Obj().parse(i))

In Obj.parse(), the element passed in is treated like this:

def parse(data):
 root=data.getroot()
 self.id=root.find("id").text
self.name=root.find("name).text

Printing the results of the above through Obj.id or Obj.name gives me
odd behavior:
print Obj.id :> <Element 'result' at [mem addr]
print self.id :> None

What is going on? Does the root change when I call find()? Why would
an Element object get used when I clearly say to use the text of the
found element? TIA.

-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


More information about the Tutor mailing list