On 1/28/11, Stefan Behnel <stefan...@behnel.de> wrote: > Hi, > > since you said that you have it working already, here are just a few > comments on your code. > > Alex Hall, 27.01.2011 23:23: >> all=root.findall("list/result") >> for i in all: >> self.mylist.append(Obj().parse(i)) > > It's uncommon to use "i" for anything but integer loop variables. And 'all' > is not a very telling name either. I'd use something like "all_results" and > "result_element" instead. True. The names in my program are different as I wrote this example from scratch to avoid the extra code that would not make sense without the whole file, but you make a good point. > > >> In Obj.parse(), the element passed in is treated like this: >> >> def parse(data): >> root=data.getroot() > > I don't understand this. You already have an Element here according to your > code above. Why do you try to call getroot() on it? Again, "data" is not > very telling. Giving it a better name will help you here. Right, I have since changed this. It would not work either way, so I thought that maybe using the same root logic on an element that I had used on the original xml would help. Obviously, it did not. :) > > >> self.id=root.find("id").text >> self.name=root.find("name).text > > There's a findtext() method on Elements for this purpose. I thought that was used to search for the text of an element? I want to get the text, whatever it may be, not search for it. Or am I misunderstanding the function? > > >> 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 > > "Obj.id" is a class attribute. "self.id" is an instance attribute. > Different things. True, and that was a typing mistake on my part. This error has since been fixed, however; it was something odd in the method that calls this one. Once that was fixed, things started working (more or less) as expected. > > >> Does the root change when I call find()? > > No. Didn't think so, but I thought I would confirm since it was the only explanation that seemed to make sense. Again, though, the problem seems to have been in the calling function. > > >> Why would >> an Element object get used when I clearly say to use the text of the >> found element? > > I don't think the code snippets you showed us above are enough to answer > this. Exactly, as the problem was in the calling function. Thanks for looking through it! > > Stefan > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor >
-- Have a great day, Alex (msg sent from GMail website) mehg...@gmail.com; http://www.facebook.com/mehgcap _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor