xml sax

Timothy Wu 2huggie at gmail.com
Thu Mar 20 01:38:37 EDT 2008


Oh right, why didn't I think of that. =)

Many thanks.

Timothy


On Thu, Mar 20, 2008 at 1:45 AM, Robert Bossy <Robert.Bossy at jouy.inra.fr>
wrote:

> Timothy Wu wrote:
> > Hi,
> >
> > I am using  xml.sax.handler.ContentHandler to parse some simple xml.
> >
> > I want to detect be able to parse the content of this tag embedded in
> > the XML.
> > <Id>174</Id>
> >
> >
> > Is the proper way of doing so involving finding the "Id" tag
> > from startElement(), setting flag when seeing one, and in characters(),
> > when seeing that flag set, save the content?
> >
> > What if multiple tags of the same name are nested at different levels
> >
> > and I want to differentiate them? I would be setting a flag for each
> level.
> > I can imagine things get pretty messy when flags are all around.
> >
> Hi,
>
> You could have a list of all opened elements from the root to the
> innermost. To keep such a list, you append the name of the element to
> this stack at the end of startElement() and pop it off at the end of
> endElement().
>
> In this way you have acces to the path of the current parser position.
> In order to differentiate between character data in Id and in Id/Id, you
> just have to iterate at the last elements of the list.
>
> Cheers,
> RB
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080320/6480ba09/attachment-0001.html>


More information about the Python-list mailing list