如何在使用SAX在Python中生成XML输出时插入DTD DOCTYPE内容

时间:2011-06-16 16:20:34

标签: python xml jython dtd

我正在尝试使用python(实际上是jython)xml.sax.saxutils.XMLGenerator生成一个大型XML文件。 我想包含DTD信息,但我无法弄清楚如何将DTD字符串传递给SAX。以下是SAX编写器类的示例:

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您可以继承XMLGenerator并添加write()方法。这是一个例子:

import sys
from xml.sax.saxutils import XMLGenerator

class MyXMLGenerator(XMLGenerator):
    def __init__(self, out=sys.stdout, encoding="UTF-8"):
        XMLGenerator.__init__(self, out, encoding)
        self.out = out

    def write (self,s):
        self.out.write(s)

def writexml(out):
    xmlwriter = MyXMLGenerator(out)
    xmlwriter.startDocument()
    xmlwriter.write("<!DOCTYPE test SYSTEM 'test.dtd'>\n")
    xmlwriter.startElement('test', {"a": "1"})
    xmlwriter.characters('abc123')
    xmlwriter.endElement('test')
    xmlwriter.endDocument()

if __name__ == '__main__':
    writexml(out=open("out.xml", "w"))

out.xml 中的结果输出:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE test SYSTEM 'test.dtd'>
<test a="1">abc123</test>