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

发布于 2024-11-15 19:05:06 字数 1372 浏览 1 评论 0原文

我正在尝试使用 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

感谢您的帮助。

I am trying to generate a large XML file using python(actually jython) xml.sax.saxutils.XMLGenerator.
I would like to include DTD information but I could not figure out how to pass the DTD string to SAX. Here is the sample 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏已如深秋| 2024-11-22 19:05:06

您可以子类化 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>

You could subclass XMLGenerator and add a write() method. Here is an example:

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"))

Resulting output in out.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE test SYSTEM 'test.dtd'>
<test a="1">abc123</test>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文