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

17 投票
6 回答
2676 浏览
提问于 2025-04-11 09:19

简单来说,作者在找一种类似于 System.Xml.XmlWriter 的东西。这是一种可以逐步写入 XML 的工具,而且不会占用太多内存。所以像 xml.dom 和 xml.dom.minidom 这些就不适合了。有没有其他的建议呢?

6 个回答

2

我想我找到了你需要的东西:

http://sourceforge.net/projects/xmlite

干杯!

15

我觉得你可以看看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

-4

xml.etree.cElementTree 是从 CPython 2.5 版本开始就自带的一个模块。它在读取和写入 XML 文件时非常快速。

撰写回答