SAX XML Parse Python error message

goldtech goldtech at worldpost.com
Mon Jul 14 00:17:12 CEST 2008


On Jul 13, 5:30 pm, Waldemar Osuch <waldemar.os... at gmail.com> wrote:
> On Jul 13, 3:00 pm, goldtech <goldt... at worldpost.com> wrote:
>
> > I would be grateful for support with the code I cited. It's not long
> > and fairly standard. I'm sure my error(s) would be glaring to more
> > experienced coders. I appreciated the "heads-up" about other options
> > but I would be grateful for help getting this code to run. Thanks
>
> Initialize self.coodinates in the __init__
> or indent the "print  self.description, str(self.coordinates)"
> one more level.
> You have to remember that "endElement" is being called on the end
> of every element.  In your case it is called by </description> but
> the parser did not see <coordinates> yet.
>
> In "def characters" you should be collecting the "ch" in a buffer.
> It may be called multiple times for the same element.
> Something like "self.description += ch" would do for starters.
>
> Also you do not need to convert self.coordinates to string before
> printing, it is already a string and even if it was not "print"
> would convert it for you.
>
> That's it for now :-) Others may spot more issues with
> your code or my response.
> On the positive side I really liked how you asked
> the question.  There was a short runnable example and traceback.
>
> Waldemar

Putting the print statements were they won't cause trouble and
using ...+= ch (vs. only =) in the character section fixed it:

...
def endElement(self, name):
...
if name == 'description':
            self.isdescriptionElement= 0
            print  self.description
        if name == 'coordinates':
            self.iscoordinatesElement = 0
            print self.coordinates
...

I need to read your answer again carefully - I don't know if what I
did is best - but it seemed to fix it. Thank you for the clear and
cogent answer.

Lee G.



More information about the Python-list mailing list