xml.sax parsing elements with the same name

amadain mfmdevine at gmail.com
Mon Jan 11 21:08:13 CET 2010


On Jan 11, 7:26 pm, John Bokma <j... at castleamber.com> wrote:
> amadain <mfmdev... at gmail.com> writes:
> > I have an event log with 100s of thousands of entries with logs of the
> > form:
>
> > <event eventTimestamp="2009-12-18T08:22:49.035"
> > uniqueId="1261124569.35725_PFS_1_1340035961">
> >    <result value="Blocked"/>
> >       <filters>
> >           <filter code="338" type="Filter_Name">
> >               <diagnostic>
> >                    <result value="Triggered"/>
> >               </diagnostic>
> >           </filter>
> >           <filter code="338" type="Filter_Name">
> >               <diagnostic>
> >                    <result value="Blocked"/>
> >               </diagnostic>
> >           </filter>
> >       </filters>
> > </event>
>
> > I am using xml.sax to parse the event log. The trouble with the file
> > above is when I parse for result value I get the last result value
> > (Blocked from above). I want to get the result value triggered (the
> > second in the event).
>
> > my code is as follows:
>
> >     def startElement(self, name, attrs):
> >         if name == 'event':
> >             self.eventTime = attrs.get('eventTimestamp',"")
> >             self.eventUniqueId = attrs.get('uniqueId', "")
> >         if name == 'result':
> >             self.resultValue = attrs.get('value',"")
> >         return
>
> >     def endElement(self, name):
> >         if name=="event":
> >             result= eval(self.filter)
> >             if result:
> >            ...
>
> > How do I get the result value I require when events have the same
> > names like above?
>
> You have to keep track if you're inside a filters section, and keep
> track of the filter elements (first, second, etc.) assuming you want the
> result value of the first filter.
>
> --
> John Bokma
>
> Read my blog:http://johnbokma.com/
> Hire me (Perl/Python):http://castleamber.com/

how do I keep track? The first result value is outside a filters
section and the rest are. Do you mean something like:

    def startElement(self, name, attrs):
        if name == 'event':
            self.eventTime = attrs.get('eventTimestamp',"")
            self.eventUniqueId = attrs.get('uniqueId', "")
        if name == 'result':
                self.resultValue = attrs.get('value',"")
        if name == filters:
            if name == 'result':
                self.resultValueF = attrs.get('value',"")
        return

A



More information about the Python-list mailing list