473,511 Members | 15,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SAX XML Parse Python error message

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\Lib\site-packages\pythonwin\pywin\framework
\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\pythonscripts\xml\parse3.py", line 43, 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 39, in endElement
print self.description, str(self.coordinates)
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(ContentHandler):

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

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

def endElement(self, name):
if name == 'Folder':
self.isFolderElement = 0
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 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>
Jul 13 '08 #1
5 2683
goldtech wrote:
My first attempt at SAX, but have an error message I need help with.
Just in case you prefer writing readable code over debugging SAX code into
existence, try lxml.

http://codespeak.net/lxml/

Here is a presentation you might find interesting.

http://codespeak.net/lxml/s5/lxml-ep2008.html

Stefan
Jul 13 '08 #2
I would be grateful for support with the code I cited. It's not long
and fairly standard. I'm sure my error(s) would be glaring to more
experienced coders. I appreciated the "heads-up" about other options
but I would be grateful for help getting this code to run. Thanks

On Jul 13, 11:47 am, Stefan Behnel <stefan...@behnel.dewrote:
goldtech wrote:
My first attempt at SAX, but have an error message I need help with.

Just in case you prefer writing readable code over debugging SAX code into
existence, try lxml.

http://codespeak.net/lxml/

Here is a presentation you might find interesting.

http://codespeak.net/lxml/s5/lxml-ep2008.html

Stefan
Jul 13 '08 #3
On Jul 13, 3:00 pm, goldtech <goldt...@worldpost.comwrote:
I would be grateful for support with the code I cited. It's not long
and fairly standard. I'm sure my error(s) would be glaring to more
experienced coders. I appreciated the "heads-up" about other options
but I would be grateful for help getting this code to run. Thanks
Initialize self.coodinates in the __init__
or indent the "print self.description, str(self.coordinates)"
one more level.
You have to remember that "endElement" is being called on the end
of every element. In your case it is called by </descriptionbut
the parser did not see <coordinatesyet.

In "def characters" you should be collecting the "ch" in a buffer.
It may be called multiple times for the same element.
Something like "self.description += ch" would do for starters.

Also you do not need to convert self.coordinates to string before
printing, it is already a string and even if it was not "print"
would convert it for you.

That's it for now :-) Others may spot more issues with
your code or my response.
On the positive side I really liked how you asked
the question. There was a short runnable example and traceback.

Waldemar
Jul 13 '08 #4
On Jul 13, 5:30 pm, Waldemar Osuch <waldemar.os...@gmail.comwrote:
On Jul 13, 3:00 pm, goldtech <goldt...@worldpost.comwrote:
I would be grateful for support with the code I cited. It's not long
and fairly standard. I'm sure my error(s) would be glaring to more
experienced coders. I appreciated the "heads-up" about other options
but I would be grateful for help getting this code to run. Thanks

Initialize self.coodinates in the __init__
or indent the "print self.description, str(self.coordinates)"
one more level.
You have to remember that "endElement" is being called on the end
of every element. In your case it is called by </descriptionbut
the parser did not see <coordinatesyet.

In "def characters" you should be collecting the "ch" in a buffer.
It may be called multiple times for the same element.
Something like "self.description += ch" would do for starters.

Also you do not need to convert self.coordinates to string before
printing, it is already a string and even if it was not "print"
would convert it for you.

That's it for now :-) Others may spot more issues with
your code or my response.
On the positive side I really liked how you asked
the question. There was a short runnable example and traceback.

Waldemar
Putting the print statements were they won't cause trouble and
using ...+= ch (vs. only =) in the character section fixed it:

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

I need to read your answer again carefully - I don't know if what I
did is best - but it seemed to fix it. Thank you for the clear and
cogent answer.

Lee G.
Jul 13 '08 #5
goldtech wrote:
I would be grateful for support with the code I cited. It's not long
and fairly standard. I'm sure my error(s) would be glaring to more
experienced coders. I appreciated the "heads-up" about other options
but I would be grateful for help getting this code to run. Thanks
For comparison, here's how an experienced Python programmer might prefer
to write your code:

import xml.etree.cElementTree as ET

description = None # most recently seen description

for event, elem in ET.parse("somefile.xml"):
if elem.tag == "description":
description = elem.text
elif elem.tag == "coordinates":
print description.strip(), elem.text.strip()

You may want to ask yourself why you prefer to struggle with obsolete,
error-prone, and slow technology when there are more efficient tools
available in Python's standard library.

(the lxml library that Stefan linked to is a superset of xml.etree, in
case you want more XML features).

</F>

Jul 15 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4091
by: Steven | last post by:
I got a "Parse error: parse error in ..." in this line: if(empty($_POST){ ..... But if I fist assign $ssn=$_POST; and then if(empty($ssn){ ... it is working. Any advice? Thanks in advance.
1
2384
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
6
9701
by: Dave Kuhlman | last post by:
Suppose that I have content that looks like what I've included at the end of this message. Is there something in the standard Python library that will help me parse it, break into the parts...
1
2473
by: H.L Bai | last post by:
hi, everybody i meet a parse error when i used the xml4c. any proposal is helpful. The error is following .../XMLRegionHandler.h:59 parse error before '*' .../XMLRegionHandler.h:60 parse...
4
5880
by: sturnfie | last post by:
Hey all, I recently came across the xml.sax libraries and am trying to use them. I am currently making a string variable, and am attempting to pass it into a parser instance as follows: def...
13
4163
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
4
12254
by: yinglcs | last post by:
Hi, I use os.system() to execute a system command in python. Can you please tell me how can I parse (in python) the output of the os.system() ? Thank you.
4
4749
by: Jean-Claude Neveu | last post by:
Hello, I am writing a Python program to check email using POP3. I've tried the sample code from python.org, and it works great. In other words, the code below successfully prints out my emails....
0
1182
by: Anish Chapagain | last post by:
Hi, i tried to compile the swig .i file but am having probel with the error: parse error before % token example.i 1. %module example 2. %{ 3. #include <header.h> 4. %}
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.