473,386 Members | 1,795 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,386 software developers and data experts.

ElementTree/DTD question

I'm trying to convert from minidom to ElementTree for handling XML,
and am having trouble with entities in DTDs. My Python script looks
like this:

----------------------------------------------------------------------

#!/usr/bin/env python

import sys, os
from elementtree import ElementTree

for filename in sys.argv[1:]:
ElementTree.parse(filename)

----------------------------------------------------------------------

My first attempt was this XML file:

----------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE lec [
<!ENTITY ldots "&#x8230;">
]>
<lec title="Introduction">
<topic title="Motivation" summary="motivation for course">
<slide>
<b1>Write an introduction&ldots;</b1>
</slide>
</topic>
</lec>

----------------------------------------------------------------------

Running "python validate.py first.xml" produces:

----------------------------------------------------------------------

Traceback (most recent call last):
File "validate.py", line 7, in ?
ElementTree.parse(filename)
File "C:\Python23\Lib\site-packages\elementtree\ElementTree.py",
line 865, in parse
tree.parse(source, parser)
File "C:\Python23\Lib\site-packages\elementtree\ElementTree.py",
line 589, in parse
parser.feed(data)
File "C:\Python23\Lib\site-packages\elementtree\ElementTree.py",
line 1160, in feed
self._parser.Parse(data, 0)
File "C:\Python23\Lib\site-packages\elementtree\ElementTree.py",
line 1113, in _default
raise expat.error(
xml.parsers.expat.ExpatError: undefined entity &ldots;: line 9, column
27

----------------------------------------------------------------------

All right, pull the DTD out, and use this XML file:

----------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE lec SYSTEM "swc.dtd">
<lec title="Introduction">
<topic title="Motivation" summary="motivation for course">
<slide>
<b1>Write an introduction&ldots;</b1>
</slide>
</topic>
</lec>

----------------------------------------------------------------------

with this minimalist DTD (saved as "swc.dtd" in the same directory as
both the XML file and the script):

----------------------------------------------------------------------

<!ENTITY ldots "&#x8230;">

----------------------------------------------------------------------

Same error; only the line number changed. Anyone know what I'm doing
wrong? (Note: minidom loads it just fine...)

Thanks,
Greg Wilson
gv******@cs.utoronto.ca
Jul 18 '05 #1
1 3060
Greg Wilson wrote:
My first attempt was this XML file:

----------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE lec [
<!ENTITY ldots "&#x8230;">
]>
<lec title="Introduction">
<topic title="Motivation" summary="motivation for course">
<slide>
<b1>Write an introduction&ldots;</b1>
</slide>
</topic>
</lec>

----------------------------------------------------------------------

Running "python validate.py first.xml" produces:

----------------------------------------------------------------------

Traceback (most recent call last):
File "validate.py", line 7, in ?
ElementTree.parse(filename)
File "C:\Python23\Lib\site-packages\elementtree\ElementTree.py",
line 865, in parse
tree.parse(source, parser)
File "C:\Python23\Lib\site-packages\elementtree\ElementTree.py",
line 589, in parse
parser.feed(data)
File "C:\Python23\Lib\site-packages\elementtree\ElementTree.py",
line 1160, in feed
self._parser.Parse(data, 0)
File "C:\Python23\Lib\site-packages\elementtree\ElementTree.py",
line 1113, in _default
raise expat.error(
xml.parsers.expat.ExpatError: undefined entity &ldots;: line 9, column
27

----------------------------------------------------------------------


looks like a bug in the Python version of elementtree. you can either switch
to cElementTree, or apply the following patch:

Index: elementtree/ElementTree.py
================================================== =================
--- elementtree/ElementTree.py (revision 2315)
+++ elementtree/ElementTree.py (working copy)
@@ -1120,7 +1120,7 @@
self._target = target
self._names = {} # name memo cache
# callbacks
- parser.DefaultHandler = self._default
+ parser.DefaultHandlerExpand = self._default
parser.StartElementHandler = self._start
parser.EndElementHandler = self._end
parser.CharacterDataHandler = self._data

(for quicker responses to elementtree questions, use the xml-sig mailing list)

</F>

Jul 18 '05 #2

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

Similar topics

7
by: Stewart Midwinter | last post by:
I want to parse a file with ElementTree. My file has the following format: <!-- file population.xml --> <?xml version='1.0' encoding='utf-8'?> <population> <person><name="joe" sex="male"...
4
by: Lonnie Princehouse | last post by:
I've run into some eccentric behavior... It appears that one of my modules is being cut off at exactly 2^14 characters when I try to import it. Has anyone else encountered this? I can't find any...
9
by: Chris Spencer | last post by:
Does anyone know how to make ElementTree preserve namespace prefixes in parsed xml files? The default behavior is to strip a document of all prefixes and then replace them autogenerated prefixes...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
15
by: Steven Bethard | last post by:
I'm having trouble using elementtree with an XML file that has some gbk-encoded text. (I can't read Chinese, so I'm taking their word for it that it's gbk-encoded.) I always have trouble with...
7
by: mirandacascade | last post by:
O/S: Windows XP Home Vsn of Python: 2.4 Copy/paste of interactive window is immediately below; the text/questions toward the bottom of this post will refer to the content of the copy/paste ...
0
by: Greg Aumann | last post by:
I am trying to write some python code for a library that reads an XML-like language from a file into elementtree data structures. Then I want to be able to read and/or modify the structure and then...
30
by: Chas Emerick | last post by:
I looked around for an ElementTree-specific mailing list, but found none -- my apologies if this is too broad a forum for this question. I've been using the lxml variant of the ElementTree API,...
2
Zver
by: Zver | last post by:
Hey All! I'm developing application that uses ElementTree for XML parsing. In python 2.5 ElementTree is part of standard "libs". Now my question is. How would you detect version of python and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
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,...

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.