基本上,类似于System.Xml.XmlWriter - 流式XML Writer,它不会产生很大的内存开销。因此排除了xml.dom和xml.dom.minidom。建议?
答案 0 :(得分:14)
我想你会发现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得到的文章的其余部分。
答案 1 :(得分:1)
答案 2 :(得分:0)
几年前,我使用4suite
中的MarkupWriter
General-purpose utility class for generating XML (may eventually be expanded to produce more output types) Sample usage: from Ft.Xml import MarkupWriter writer = MarkupWriter(indent=u"yes") writer.startDocument() writer.startElement(u'xsa') writer.startElement(u'vendor') #Element with simple text (#PCDATA) content writer.simpleElement(u'name', content=u'Centigrade systems') #Note writer.text(content) still works writer.simpleElement(u'email', content=u"info@centigrade.bogus") writer.endElement(u'vendor') #Element with an attribute writer.startElement(u'product', attributes={u'id': u"100\u00B0"}) #Note writer.attribute(name, value, namespace=None) still works writer.simpleElement(u'name', content=u"100\u00B0 Server") #XML fragment writer.xmlFragment('<version>1.0</version><last-release>20030401</last-release>') #Empty element writer.simpleElement(u'changes') writer.endElement(u'product') writer.endElement(u'xsa') writer.endDocument() Note on the difference between 4Suite writers and printers Writer - module that exposes a broad public API for building output bit by bit Printer - module that simply takes a DOM and creates output from it as a whole, within one API invokation
答案 3 :(得分:-1)
lxml我的成绩总是很好。安装很麻烦,因为它主要是libxml2的包装器,但是lxml.etree
树对象有一个.write()
方法,它将类似文件的对象传输到。
from lxml.etree import XML
tree = XML('<root><a><b/></a></root>')
tree.write(your_file_object)
答案 4 :(得分:-2)
ElementTree的第二次投票(cElementTree是一个更快的C实现,比如cPickle vs pickle)。这里有一些简短的示例代码,您可以查看它以了解它是如何工作的:http://effbot.org/zone/element-index.htm (这是Fredrik Lundh,他首先编写了这个模块。它非常好用2.5 :-)编写标准库。
答案 5 :(得分:-4)
xml.etree.cElementTree ,自2.5以来包含在CPython的默认分发中。快速读取和写入XML。