SAX XML Parse Python error message
Hi,
My first attempt at SAX, but have an error message I need help with.
I cite the error message, code, and xml below.
Be grateful if anyone can tell me what the fix is.
Thanks.
Traceback (most recent call last):
File "C:\Python24\Li b\site-packages\python win\pywin\frame work
\scriptutils.py ", line 310, in RunScript
exec codeObject in __main__.__dict __
File "C:\pythonscrip ts\xml\parse3.p y", line 43, in ?
parser.parse(r' C:\perlscripts\ xml\Document2.k ml')
File "C:\Python24\li b\xml\sax\expat reader.py", line 107, in parse
xmlreader.Incre mentalParser.pa rse(self, source)
File "C:\Python24\li b\xml\sax\xmlre ader.py", line 123, in parse
self.feed(buffe r)
File "C:\Python24\li b\xml\sax\expat reader.py", line 207, in feed
self._parser.Pa rse(data, isFinal)
File "C:\Python24\li b\xml\sax\expat reader.py", line 303, in
end_element
self._cont_hand ler.endElement( name)
File "C:\pythonscrip ts\xml\parse3.p y", line 39, in endElement
print self.descriptio n, str(self.coordi nates)
AttributeError: G_Handler instance has no attribute 'coordinates'
Code:
from xml.sax import make_parser
from xml.sax.handler import ContentHandler
import string
class G_Handler(Conte ntHandler):
def __init__ (self):
self.isFolderEl ement = 0
self.isdescript ionElement = 0
self.iscoordina tesElement = 0
def startElement(se lf, name , attrs):
if name == 'Folder':
self.isFolderEl ement= 1
self.Folder = ""
if name == 'description':
self.isdescript ionElement= 1
self.descriptio n = ""
if name == 'coordinates':
self.iscoordina tesElement = 1
self.coordinate s = ""
def characters (self, ch):
if self.isFolderEl ement == 1:
self.Folder = ch
if self.isdescript ionElement == 1:
self.descriptio n = ch
if self.iscoordina tesElement == 1:
self.coordinate s = ch
def endElement(self , name):
if name == 'Folder':
self.isFolderEl ement = 0
if name == 'description':
self.isdescript ionElement= 0
if name == 'coordinates':
self.iscoordina tesElement = 0
print self.descriptio n, str(self.coordi nates)
parser = make_parser()
parser.setConte ntHandler(G_Han dler())
parser.parse(r' C:\perlscripts\ xml\Document2.k ml')
<?xml version="1.0" encoding="UTF-8"?>
<Folder>
<description>
abc
</description>
<coordinates>
-84.4, 33.7
</coordinates>
<description>
abc
</description>
<coordinates>
-86.7, 36.1
</coordinates>
</Folder>
Hi,
My first attempt at SAX, but have an error message I need help with.
I cite the error message, code, and xml below.
Be grateful if anyone can tell me what the fix is.
Thanks.
>>>
File "C:\Python24\Li b\site-packages\python win\pywin\frame work
\scriptutils.py ", line 310, in RunScript
exec codeObject in __main__.__dict __
File "C:\pythonscrip ts\xml\parse3.p y", line 43, in ?
parser.parse(r' C:\perlscripts\ xml\Document2.k ml')
File "C:\Python24\li b\xml\sax\expat reader.py", line 107, in parse
xmlreader.Incre mentalParser.pa rse(self, source)
File "C:\Python24\li b\xml\sax\xmlre ader.py", line 123, in parse
self.feed(buffe r)
File "C:\Python24\li b\xml\sax\expat reader.py", line 207, in feed
self._parser.Pa rse(data, isFinal)
File "C:\Python24\li b\xml\sax\expat reader.py", line 303, in
end_element
self._cont_hand ler.endElement( name)
File "C:\pythonscrip ts\xml\parse3.p y", line 39, in endElement
print self.descriptio n, str(self.coordi nates)
AttributeError: G_Handler instance has no attribute 'coordinates'
>>>
Code:
from xml.sax import make_parser
from xml.sax.handler import ContentHandler
import string
class G_Handler(Conte ntHandler):
def __init__ (self):
self.isFolderEl ement = 0
self.isdescript ionElement = 0
self.iscoordina tesElement = 0
def startElement(se lf, name , attrs):
if name == 'Folder':
self.isFolderEl ement= 1
self.Folder = ""
if name == 'description':
self.isdescript ionElement= 1
self.descriptio n = ""
if name == 'coordinates':
self.iscoordina tesElement = 1
self.coordinate s = ""
def characters (self, ch):
if self.isFolderEl ement == 1:
self.Folder = ch
if self.isdescript ionElement == 1:
self.descriptio n = ch
if self.iscoordina tesElement == 1:
self.coordinate s = ch
def endElement(self , name):
if name == 'Folder':
self.isFolderEl ement = 0
if name == 'description':
self.isdescript ionElement= 0
if name == 'coordinates':
self.iscoordina tesElement = 0
print self.descriptio n, str(self.coordi nates)
parser = make_parser()
parser.setConte ntHandler(G_Han dler())
parser.parse(r' C:\perlscripts\ xml\Document2.k ml')
<?xml version="1.0" encoding="UTF-8"?>
<Folder>
<description>
abc
</description>
<coordinates>
-84.4, 33.7
</coordinates>
<description>
abc
</description>
<coordinates>
-86.7, 36.1
</coordinates>
</Folder>
Comment