Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pyhton XML SAX help

Status
Not open for further replies.

leegold2

Technical User
Oct 10, 2004
116
Hi,

I'm new at this- trying Python SAX parser. I'm obviously doing something wrong. Below please find the error message, code and the xml. Be grateful to learn what I'm doing wrong - it's probably a basic concept...

error message:

Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\pythonscripts\xml\parse3.py", line 37, in ?
parser.parse(r'C:\perlscripts\xml\Document2.kml')
File "C:\Python24\lib\xml\sax\expatreader.py", line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
File "C:\Python24\lib\xml\sax\xmlreader.py", line 123, in parse
self.feed(buffer)
File "C:\Python24\lib\xml\sax\expatreader.py", line 207, in feed
self._parser.Parse(data, isFinal)
File "C:\Python24\lib\xml\sax\expatreader.py", line 303, in end_element
self._cont_handler.endElement(name)
File "C:\pythonscripts\xml\parse3.py", line 31, in endElement
print self.description, str(self.coordinates)
AttributeError: G_Handler instance has no attribute 'description'
>>>

Code:

from xml.sax import make_parser
from xml.sax.handler import ContentHandler
import string

class G_Handler(ContentHandler):

def __init__ (self):
self.isdescriptionElement = 0
self.iscoordinatesElement = 0

def startElement(self, name , attrs):
if name == 'description':
self.isdescriptionElement= 1
self.description = ""
if name == 'coordinates':
self.iscoordinatesElement = 1
self.coordinates = ""


def characters (self, ch):
if self.isdescriptionElement == 1:
self.description = ch
if self.iscoordinatesElement == 1:
self.coordinates = ch

def endElement(self, name):
if name == 'description':
self.isdescriptionElement= 0
if name == 'coordinates':
self.iscoordinatesElement = 0
print self.description, str(self.coordinates)

parser = make_parser()
parser.setContentHandler(G_Handler())
parser.parse(r'C:\perlscripts\xml\Document2.kml')


XML:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns=" <Document>

<name>Paths</name>
<description>Path from Atlanta to Nashville</description>
<Style id="yellowLineGreenPoly">
<LineStyle>
<color>7f00ffff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>

<Placemark>
<name>Atlanta to Nashville</name>
<description>
abc
</description>
<styleUrl>#yellowLineGreenPoly</styleUrl>
<Point>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<coordinates>
84.4,33.7
</coordinates>
</Point>
</Placemark>



<Placemark>
<name>Atlanta to Nashville</name>
<description>
abc
</description>
<styleUrl>#yellowLineGreenPoly</styleUrl>
<Point>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<coordinates>
-86.7,36.1
</coordinates>
</Point>
</Placemark>

</Document>
</kml>
 
Python says AttributeError: G_Handler instance has no attribute 'description' and exactly that he means :)

You create the attribute self.description = "" with the method startElement() only if the name == 'description'.
That is, if the name != 'description' the instance of the class G_handler does not have such an attribute.
And if you look inside in your xml-file, you will see, that there are much more tags - not only 'description' and 'coordinates'

So you need to change the method endElement() to this
Code:
    def endElement(self, name):
        if name == 'description':
            self.isdescriptionElement= 0
            print  self.description
        if name == 'coordinates':
            self.iscoordinatesElement = 0
            print  str(self.coordinates)
Now the Error Mesage is gone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top