xml.sax parsing elements with the same name

John Bokma john at castleamber.com
Mon Jan 11 14:26:30 EST 2010


amadain <mfmdevine 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/



More information about the Python-list mailing list