XML writing tools for Python

后端 未结 8 1072
眼角桃花
眼角桃花 2020-12-23 09:30

I\'m currently trying ElementTree and it looks fine, it escapes HTML entities and so on and so forth. Am I missing something truly wonderful I haven\'t heard of?

Thi

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-23 10:21

    For anyone encountering this now, there's actually a way to do this hidden away in Python's standard library in xml.sax.utils.XMLGenerator. Here's an example of it in action:

    >>> from xml.sax.saxutils import XMLGenerator
    >>> import StringIO
    >>> w = XMLGenerator(out, 'utf-8')
    >>> w.startDocument()
    >>> w.startElement("test", {'bar': 'baz'})
    >>> w.characters("Foo")
    >>> w.endElement("test")
    >>> w.endDocument()
    >>> print out.getvalue()
    
    Foo
    

提交回复
热议问题