I am trying to create a large XML file using python (actually jython) xml.sax.saxutils.XMLGenerator. I would like to include the DTD information, but I could not figure out how to pass the SAX DTD string. Here is an example SAX writer class:
from xml.sax.saxutils import XMLGenerator
class xml_writer:
def __init__(self, output, encoding):
"""
an XML writer object that generate xml output to a file
"""
xmlwriter = XMLGenerator(output, encoding)
xmlwriter.startDocument()
self._writer = xmlwriter
self._output = output
self._encoding = encoding
self.attr_vals = {}
self.attr_qnames = {}
return
def start_elem(self, name):
name = unicode(name)
attrs = AttributesNSImpl(self.attr_vals, self.attr_qnames)
self._writer.startElementNS((None, name), name, attrs)
self._writer._out.write('\n')
self.attr_qnames = {}
self.attr_vals = {}
def end_elem(self, name):
name = unicode(name)
self._writer.endElementNS((None, name), name)
self._writer._out.write('\n')
def setAttribute(self, aname, value):
aname = unicode(aname)
value = unicode(value)
self.attr_vals[(None, aname)] = value
self.attr_qnames[(None, aname)] = aname
def close(self):
"""
Clean up
"""
self._writer.endDocument()
return
Appreciate your help.
source
share