使用SAX在Python中生成XML输出时如何插入DTD DOCTYPE内容
作者:互联网
我正在尝试使用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
感谢您的帮助.
解决方法:
您可以继承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>
标签:jython,dtd,xml,python 来源: https://codeday.me/bug/20191208/2090232.html