diff options
author | Lars Gustäbel <lars@gustaebel.de> | 2000-09-21 08:25:28 (GMT) |
---|---|---|
committer | Lars Gustäbel <lars@gustaebel.de> | 2000-09-21 08:25:28 (GMT) |
commit | c5cec51b95dadf0740c0c10260ae5011a4bb3ea5 (patch) | |
tree | 202c313c4c9723da72e5acd9e18203c2d68f7333 /Lib/xml/sax | |
parent | b4d6bb098776de48275c2e422c608ddfd85f4198 (diff) | |
download | cpython-c5cec51b95dadf0740c0c10260ae5011a4bb3ea5.zip cpython-c5cec51b95dadf0740c0c10260ae5011a4bb3ea5.tar.gz cpython-c5cec51b95dadf0740c0c10260ae5011a4bb3ea5.tar.bz2 |
Updated XMLGenerator to new DocumentHandler interface (patch 101572).
Diffstat (limited to 'Lib/xml/sax')
-rw-r--r-- | Lib/xml/sax/saxutils.py | 41 |
1 files changed, 30 insertions, 11 deletions
diff --git a/Lib/xml/sax/saxutils.py b/Lib/xml/sax/saxutils.py index cd6426a..d288e9d 100644 --- a/Lib/xml/sax/saxutils.py +++ b/Lib/xml/sax/saxutils.py @@ -23,37 +23,50 @@ def escape(data, entities={}): class XMLGenerator(handler.ContentHandler): - def __init__(self, out=None): + def __init__(self, out=None, encoding="iso-8859-1"): if out is None: import sys out = sys.stdout handler.ContentHandler.__init__(self) self._out = out + self._ns_contexts = [{}] # contains uri -> prefix dicts + self._current_context = self._ns_contexts[-1] + self._encoding = encoding # ContentHandler methods def startDocument(self): - self._out.write('<?xml version="1.0" encoding="iso-8859-1"?>\n') + self._out.write('<?xml version="1.0" encoding="%s"?>\n' % + self._encoding) def startPrefixMapping(self, prefix, uri): - pass + self._ns_contexts.append(self._current_context.copy()) + self._current_context[uri] = prefix def endPrefixMapping(self, prefix): - pass + del self._current_context[-1] def startElement(self, name, attrs): - if type(name) is type(()): - uri, localname, prefix = name - name = "%s:%s"%(prefix,localname) self._out.write('<' + name) for (name, value) in attrs.items(): self._out.write(' %s="%s"' % (name, escape(value))) self._out.write('>') - + def endElement(self, name): - # FIXME: not namespace friendly yet self._out.write('</%s>' % name) + def startElementNS(self, name, qname, attrs): + name = self._current_context[name[0]] + ":" + name[1] + self._out.write('<' + name) + for (name, value) in attrs.items(): + name = self._current_context[name[0]] + ":" + name[1] + self._out.write(' %s="%s"' % (name, escape(value))) + self._out.write('>') + + def endElementNS(self, name, qname): + name = self._current_context[name[0]] + ":" + name[1] + self._out.write('</%s>' % name) + def characters(self, content): self._out.write(escape(content)) @@ -103,8 +116,14 @@ class XMLFilterBase: def startElement(self, name, attrs): self._cont_handler.startElement(name, attrs) - def endElement(self, name, qname): - self._cont_handler.endElement(name, qname) + def endElement(self, name): + self._cont_handler.endElement(name) + + def startElementNS(self, name, qname, attrs): + self._cont_handler.startElement(name, attrs) + + def endElementNS(self, name, qname): + self._cont_handler.endElementNS(name, qname) def characters(self, content): self._cont_handler.characters(content) |