ElementTree/DTD question

G

Greg Wilson

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 "舰">
]>
<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 "舰">

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

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

Thanks,
Greg Wilson
(e-mail address removed)
 
F

Fredrik Lundh

Greg said:
My first attempt was this XML file:

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE lec [
<!ENTITY ldots "舰">
]>
<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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top