Parsing XML with SAX and Python - The Heart of the Code (Page 4 of 7 )
The ‘heart’ of our code is our handler class, which uses the ContentHandler object:
This class has a constructor where we initialize the variables to be used in the rest of our code. Of course, we want to make sure we save the value of our search term in a variable. We also define two flags (isPointsElement and isReboundsElement) and give them the value of 0. As soon as the content of an element of interest is reached (for the ‘rebounds’ and ‘points’ elements) these flags will be set to 1.
I would like to mention here that all these comments and snippets of code are outlined to the left, but as you probably know already, Python is sensitive to tabbing of blocks of code. Take a look at the full code example at the end if in doubt about the correct syntax, order and tabbing.
Now we need to work with the elements of our XML document. This is why we will use the methods startElement() and endElement(). These methods will be invoked each and every time a new element in the document is, respectively, opened or closed.
The parameter name of the startElement() function contains the name of the element type. If you look again at our XML example, you will see that the ‘player’ element has some properties assigned. We obtain their values by using the get() method of the Attributes interface, then we save these values for later.