473,651 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\elemen ttree\
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.Ele mentTree as ElemTree
badxml = '<response>abc </responsez>'
root = ElemTree.XML(ba dxml) Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
File "C:\Python24\Li b\site-packages\elemen ttree\ElementTr ee.py", line
960, in XML
parser.feed(tex t)
File "C:\Python24\Li b\site-packages\elemen ttree\ElementTr ee.py", line
1242, in feed
self._parser.Pa rse(data, 0)
ExpatError: mismatched tag: line 1, column 15 try: .... root = ElemTree.XML(ba dxml)
.... except:
.... print 'some exception raised'
....
some exception raised try:

.... root = ElemTree.XML(ba dxml)
.... except ExpatError:
.... print 'ExpatError exception raised'
....
Traceback (most recent call last):
File "<interacti ve 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 3955
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.exp at. So, first you
need to import that package:

import xml.parsers.exp at

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

from xml.parsers.exp at 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...man y 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.exp at.

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.)...i t located the file Mr. Boddie
mentioned...\li b\xml\parsers\e xpat.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.exp at.
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...\li b\xml\parsers\e xpat.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
3652
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 UltraEdit with that xml file, and highlighting the offending line (keep in mind UEdit can take command line argument with the row and column) When I let the exception be caught by default, I notice that I get:
8
2718
by: StepH | last post by:
Hi, I've this module : from optparse import OptionParser import re _version = "P1" def writeVar(f, line):
4
8587
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 comments and PI which have purposely been left out). It isn't however the case when it comes to CDATA handling. I have the following code: text="""<html><head> <title>Document</title> </head>
1
3962
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> <SpecificInformation>
9
2534
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 the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all? Just curious...
2
1960
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 the XML document 2) The inherited code is somewhat 'brittle' in that some well-formed XML documents are not correctly processed by the code; the brittleness is caused by how the parser portion of the code handles whitespace. 3) I would like to...
3
5555
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 defined".
17
8465
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 file under linux python 2.4 (same problem with windows 2.5, tried with the first example) : #Python interpreter memory usage : 1.1 Mb private, 1.4 Mb shared #Using http://www.pixelbeat.org/scripts/ps_mem.py to get memory information
3
2080
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 available, gives me ... ElementTree(sectionElement).write("section.xml") TypeError: 'module' object is not callable I'm new to python, so I think I'm doing something wrong.. any
0
8347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8694
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8457
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6157
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
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.