符合pythonic标准的轻量级流式xml编写器
streamingxmlwriter的Python项目详细描述
streamingxmlwriter
符合pythonic标准的轻量级流式xml编写器。
fromioimportBytesIOimportstreamingxmlwriterstream=BytesIO()withstreamingxmlwriter.from_stream(stream)aswriter:writer.start_namespace('myns','http://mynamespace.org/')withwriter.element('myns:root',{'att1':'1'}):withwriter.element('myns:child1'):writer.characters('text content')writer.comment(' a comment ')withwriter.element('myns:child2'):writer.characters('text content')# shortcut for elements containing a single text nodewriter.text_element('myns:child3','text content',{'att2':'2'})
有关更多API示例,请参阅 StreamingXMLWriter类在core.py中。
在引擎盖下,它生成标准xml.sax.saxutils.xmlgenerator的sax事件。 它还提供了一个from_sax_handler构造函数,因此它还可以用来发出 sax事件用于输出到io流以外的其他目的
支持Python2(2.7+)和Python3(3.3+)
支持的XML功能包括:
- elements
- attributes
- text
- processing instructions
- comments
- selectable encoding
- namespaces for elements and attributes, with or without prefix
不支持的XML功能(目前):
- DOCTYPE declaration
- entities
- CDATA sections