xml-filter with XMLFilterBase() and XMLGenerator() shuffles attributes

Dmitry Teslenko dteslenko at gmail.com
Thu Dec 20 13:35:55 EST 2007


Hello!
I've made a trivial xml filter to modify some attributes on-the-fly:

...
from __future__ import with_statement
import os
import sys

from xml import sax
from xml.sax import saxutils

class ReIdFilter(saxutils.XMLFilterBase):
	def __init__(self, upstream, downstream):
		saxutils.XMLFilterBase.__init__(self, upstream)

		self.__downstream = downstream
		return

	def startElement(self, name, attrs):
		self.__downstream.startElement(name, attrs)
		return

	def startElementNS(self, name, qname, attrs):
		self.__downstream.startElementNS(name, qname, attrs)
		return

	def endElement(self, name):
		self.__downstream.endElement(name)
		return

	def endElementNS(self, name, qname):
		self.__downstream.endElementNS(name, qname)
		return

	def processingInstruction(self, target, body):
		self.__downstream.processingInstruction(target, body)
		return

	def comment(self, body):
		self.__downstream.comment(body)
		return

	def characters(self, text):
		self.__downstream.characters(text)
		return

	def ignorableWhitespace(self, ws):
		self.__downstream.ignorableWhitespace(ws)
		return

...
	with open(some_file_path, 'w') as f:
		parser = sax.make_parser()
		downstream_handler = saxutils.XMLGenerator(f, 'cp1251')
		filter_handler = ReIdFilter(parser, downstream_handler)
		filter_handler.parse(file_path)

I want prevent it from shuffling attributes, i.e. preserve original
file's attribute order. Is there any ContentHandler.features*
responsible for that?



More information about the Python-list mailing list