当前位置:  开发笔记 > 编程语言 > 正文

从Python输出XML最容易的非内存密集型方法是什么?

如何解决《从Python输出XML最容易的非内存密集型方法是什么?》经验,为你挑选了1个好方法。

基本上,类似于System.Xml.XmlWriter - 流式XML Writer,它不会产生大量的内存开销.因此排除了xml.dom和xml.dom.minidom.建议?



1> Tom Dunham..:

我想你会发现xml.sax.saxutils中的XMLGenerator最接近你想要的东西.

import time
from xml.sax.saxutils import XMLGenerator
from xml.sax.xmlreader import AttributesNSImpl

LOG_LEVELS = ['DEBUG', 'WARNING', 'ERROR']


class xml_logger:
    def __init__(self, output, encoding):
        """
        Set up a logger object, which takes SAX events and outputs
        an XML log file
        """
        logger = XMLGenerator(output, encoding)
        logger.startDocument()
        attrs = AttributesNSImpl({}, {})
        logger.startElementNS((None, u'log'), u'log', attrs)
        self._logger = logger
        self._output = output
        self._encoding = encoding
        return

    def write_entry(self, level, msg):
        """
        Write a log entry to the logger
        level - the level of the entry
        msg   - the text of the entry.  Must be a Unicode object
        """
        #Note: in a real application, I would use ISO 8601 for the date
        #asctime used here for simplicity
        now = time.asctime(time.localtime())
        attr_vals = {
            (None, u'date'): now,
            (None, u'level'): LOG_LEVELS[level],
            }
        attr_qnames = {
            (None, u'date'): u'date',
            (None, u'level'): u'level',
            }
        attrs = AttributesNSImpl(attr_vals, attr_qnames)
        self._logger.startElementNS((None, u'entry'), u'entry', attrs)
        self._logger.characters(msg)
        self._logger.endElementNS((None, u'entry'), u'entry')
        return

    def close(self):
        """
        Clean up the logger object
        """
        self._logger.endElementNS((None, u'log'), u'log')
        self._logger.endDocument()
        return

if __name__ == "__main__":
    #Test it out
    import sys
    xl = xml_logger(sys.stdout, 'utf-8')
    xl.write_entry(2, u"Vanilla log entry")
    xl.close()   

您可能希望查看我从http://www.xml.com/pub/a/2003/03/12/py-xml.html获得的文章的其余部分.

推荐阅读
jerry613
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有