xml.sax parsing elements with the same name

Adam Tauno Williams awilliam at whitemice.org
Fri Jan 15 20:08:25 CET 2010


On Mon, 2010-01-11 at 13:24 -0800, amadain wrote:
> On Jan 11, 9:03 pm, John Bokma <j... at castleamber.com> wrote:
> > amadain <mfmdev... at gmail.com> writes:
> > I was thinking about something like:
> > self.filterIndex = 0
> > in startElement:
> >     if name == 'filter':
> >        self.filterIndex += 1
> >        return
> >     if name == 'result' and self.filterIndex == 1:
> >        ...  = attrs.get('value', '')
> > in EndElement
> >    if name == 'filters':
> >       self.filterIndex = 0
> > If you want the result of the first filter in filters
> Thank you. I will try that

If you document is reasonably complex I usually define some modes like:

BPML_BOOTSTRAP_MODE  = 0
BPML_PACKAGE_MODE    = 1
BPML_PROCESS_MODE    = 2
BPML_CONTEXT_MODE    = 3
....
BPML_EVENT_MODE      = 10
BPML_FAULTS_MODE     = 11
BPML_ATTRIBUTES_MODE = 12

- so I can self.mode.append(BPML_PROCESS_MODE) when I enter an element
(startElement) and self.mode = self.mode[:-1] when I leave an element
(endElement).  This provides you with a complete 'stack trace' of how
you got where you are and still lets you efficiently process the stream
[verses using evil document model].  In startElement you can check the
current mode and tag with something like -
...
elif (name == 'event' and self.mode[-1] -- BPML_PROCESS_MODE):
...

-- 
OpenGroupware developer: awilliam at whitemice.org
<http://whitemiceconsulting.blogspot.com/>
OpenGroupare & Cyrus IMAPd documenation @
<http://docs.opengroupware.org/Members/whitemice/wmogag/file_view>




More information about the Python-list mailing list