生成xml的最佳方式? [英] Best way to generate xml?
本文介绍了生成xml的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个 web api,需要一种很好的方法来非常快速地生成一些格式良好的 xml.我在 python 中找不到任何好的方法.
I'm creating an web api and need a good way to very quickly generate some well formatted xml. I cannot find any good way of doing this in python.
注意:有些库看起来很有前途,但要么缺少文档,要么只输出到文件.
Note: Some libraries look promising but either lack documentation or only output to files.
推荐答案
使用 lxml:
from lxml import etree
# create XML
root = etree.Element('root')
root.append(etree.Element('child'))
# another child with text
child = etree.Element('child')
child.text = 'some text'
root.append(child)
# pretty string
s = etree.tostring(root, pretty_print=True)
print s
输出:
<root>
<child/>
<child>some text</child>
</root>
有关详细信息,请参阅教程.
See the tutorial for more information.
这篇关于生成xml的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文