473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

handling ExpatError exception raised from ElementTree.XML() method

Verion of Python: 2.4
O/S: Windows XP
ElementTree resides in the c:\python24\lib\site-packages\elementtree\
folder

When a string that does not contain well-formed XML is passed as an
argument to the XML() method in ElementTree.py, an ExpatError exception
is raised. I can trap the exception with a try/except where the except
does not specify a specific exception, but I cannot figure out how to
construct the except clause in a try/except statement to catch the
ExpatError exception. Interactive window illustrates
import elementtree.ElementTree as ElemTree
badxml = '<response>abc</responsez>'
root = ElemTree.XML(badxml) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python24\Lib\site-packages\elementtree\ElementTree.py", line
960, in XML
parser.feed(text)
File "C:\Python24\Lib\site-packages\elementtree\ElementTree.py", line
1242, in feed
self._parser.Parse(data, 0)
ExpatError: mismatched tag: line 1, column 15 try: .... root = ElemTree.XML(badxml)
.... except:
.... print 'some exception raised'
....
some exception raised try:

.... root = ElemTree.XML(badxml)
.... except ExpatError:
.... print 'ExpatError exception raised'
....
Traceback (most recent call last):
File "<interactive input>", line 3, in ?
NameError: name 'ExpatError' is not defined

I'm guessing that I need to define/describe the ExpatError exception
class and then refer to that defined exception class after the keyword
'except' and before the ':', but I cannot figure out how to do that.

Oct 27 '05 #1
3 3919
mirandacasc...@yahoo.com wrote:

NameError: name 'ExpatError' is not defined

I'm guessing that I need to define/describe the ExpatError exception
class and then refer to that defined exception class after the keyword
'except' and before the ':', but I cannot figure out how to do that.


You just need to know how to reference ExpatError yourself. Doing a
grep on the site-packages directory revealed that the most likely
location of the ExpatError class is xml.parsers.expat. So, first you
need to import that package:

import xml.parsers.expat

Then, you can refer to the class as an attribute of that package.
Alternatively...

from xml.parsers.expat import ExpatError

....lets you use the name directly.

I think that it can be useful when making a library to make common
exception classes attributes of certain principal classes/objects, in
order to simplify this kind of situation and to avoid the need to find
the originating module of some exception or other.

Paul

Oct 27 '05 #2
Mr. Boddie's suggestions work as indicated...many thanks.

It's not clear how a grep of the site-packages directory revealed the
most likely location of the ExpatError class is xml.parsers.expat.

Using XP's search utility, I searched for any files within the
c:\python24\ folder structure that had 'ExpatError' somewhere in the
file (I specified a case sensitive search.) That search found nothing.
I modified the search to look for any files with 'expat' (not case
sensitive.)...it located the file Mr. Boddie
mentioned...\lib\xml\parsers\expat.py. That file has only a few lines
of code...it appears to import all the objects from what I'm guessing
is the pyexpat extension module. I'm guessing that ExpatError is
embedded within that extension module...is that a correct guess?

It's not clear how Mr. Boddie was able to surmise where ExpatError
resides given that a case sensitive search of the folder structure
didn't find any files containing 'ExpatError'.

Oct 28 '05 #3
mirandacasc...@yahoo.com wrote:

It's not clear how a grep of the site-packages directory revealed the
most likely location of the ExpatError class is xml.parsers.expat.
Here's what I did:

grep -e ExpatError -r /usr/lib/python2.4/site-packages/

What is interesting is that most of the "hits" belonged to packages
which you may not have on your system: pyxdg and smartpm, for example,
which mention the ExpatError class. I did get the following message,
though:

Binary file
/usr/lib/python2.4/site-packages/_xmlplus/parsers/pyexpat.so matches
...it located the file Mr. Boddie mentioned...\lib\xml\parsers\expat.py. That file has
only a few lines of code...it appears to import all the objects from what I'm guessing
is the pyexpat extension module. I'm guessing that ExpatError is
embedded within that extension module...is that a correct guess?


I believe so.

Paul

Oct 31 '05 #4

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

Similar topics

3
by: python newbie | last post by:
Hey Straightforward question: I'm opening an xml file with minidom. When I have a mismatch in the XML file, I simply want my python script to not only stop gracefully, but also to call up...
8
by: StepH | last post by:
Hi, I've this module : from optparse import OptionParser import re _version = "P1" def writeVar(f, line):
4
by: alainpoint | last post by:
I am experimenting with ElementTree and i came accross some (apparently) weird behaviour. I would expect a piece of XML to be read, parsed and written back without corruption (except for the...
1
by: mirandacascade | last post by:
I do not understand how to use the find() method in ElementTree. The file 'sample.xml' is: <?xml version="1.0"?> <SampleRoot> <Header> <Product>FindMystery</Product> </Header>...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
2
by: mirandacascade | last post by:
Situation is this: 1) I have inherited some python code that accepts a string object, the contents of which is an XML document, and produces a data structure that represents some of the content of...
3
by: Shuaib | last post by:
Hey! I am getting this exception. xml.parsers.expat.ExpatError But I am not able to catch it with "except xml.parsers.expat.ExpatError:" It says "NameError: global name 'xml' is not...
17
by: frederic.pica | last post by:
Greets, I've some troubles getting my memory freed by python, how can I force it to release the memory ? I've tried del and gc.collect() with no success. Here is a code sample, parsing an XML...
3
by: gray.bowman | last post by:
I'm messing around with trying to write an xml file using xml.etree.ElementTree. All the examples on the internet show the use of ElementTree.write(), although when I try to use it it's not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.