[issue8583] Hardcoded namespace_separator in the cElementTree.XMLParser
Dmitry Chichkov
report at bugs.python.org
Sat May 1 01:25:58 CEST 2010
Dmitry Chichkov <dchichkov at gmail.com> added the comment:
And obviously iterparse can be either overridden in the local user code or patched in the library. Here's the iterparse code/test code:
import cElementTree
from cStringIO import StringIO
class iterparse(object):
root = None
def __init__(self, file, events=None, namespace_separator = "}"):
if not hasattr(file, 'read'):
file = open(file, 'rb')
self._file = file
self._events = events
self._namespace_separator = namespace_separator
def __iter__(self):
events = []
b = cElementTree.TreeBuilder()
p = cElementTree.XMLParser(b, namespace_separator= \
self._namespace_separator)
p._setevents(events, self._events)
while 1:
data = self._file.read(16384)
if not data:
break
p.feed(data)
for event in events:
yield event
del events[:]
root = p.close()
for event in events:
yield event
self.root = root
x = """<root xmlns="http://www.very_long_url.com"><child>text</child></root>"""
context = iterparse(StringIO(x), events=("start", "end", "start-ns"))
for event, elem in context: print event, elem
context = iterparse(StringIO(x), events=("start", "end", "start-ns"), namespace_separator = None)
for event, elem in context: print event, elem
It produces:
start-ns ('', 'http://www.very_long_url.com')
start <Element '{http://www.very_long_url.com}root' at 0xb7ccf650>
start <Element '{http://www.very_long_url.com}child' at 0xb7ccf5a8>
end <Element '{http://www.very_long_url.com}child' at 0xb7ccf5a8>
end <Element '{http://www.very_long_url.com}root' at 0xb7ccf650>
start <Element 'root' at 0xb7ccf620>
start <Element 'child' at 0xb7ccf458>
end <Element 'child' at 0xb7ccf458>
end <Element 'root' at 0xb7ccf620>
Note the absence of URIs and ignored start-ns events in the 'space_separator = None' version.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8583>
_______________________________________
More information about the Python-bugs-list
mailing list