Python 生成xml的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3844360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to generate xml?
提问by Joshkunz
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.
我正在创建一个 web api,需要一种很好的方法来非常快速地生成一些格式良好的 xml。我在 python 中找不到任何这样做的好方法。
Note: Some libraries look promising but either lack documentation or only output to files.
注意:有些库看起来很有前途,但要么缺少文档,要么只输出到文件。
采纳答案by ars
Using lxml:
使用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
Output:
输出:
<root>
<child/>
<child>some text</child>
</root>
See the tutorialfor more information.
有关更多信息,请参阅教程。
回答by Anurag Uniyal
ElementTreeis a good module for reading xml and writing too e.g.
ElementTree是一个很好的模块,用于读取 xml 和写入,例如
from xml.etree.ElementTree import Element, SubElement, tostring
root = Element('root')
child = SubElement(root, "child")
child.text = "I am a child"
print tostring(root)
Output:
输出:
<root><child>I am a child</child></root>
See this tutorialfor more details and how to pretty print.
有关更多详细信息以及如何漂亮打印,请参阅本教程。
Alternatively if your XML is simple, do not underestimate the power of string formatting :)
或者,如果您的 XML 很简单,请不要低估字符串格式的力量:)
xmlTemplate = """<root>
<person>
<name>%(name)s</name>
<address>%(address)s</address>
</person>
</root>"""
data = {'name':'anurag', 'address':'Pune, india'}
print xmlTemplate%data
Output:
输出:
<root>
<person>
<name>anurag</name>
<address>Pune, india</address>
</person>
</root>
You can use string.Template or some template engine too, for complex formatting.
您也可以使用 string.Template 或一些模板引擎来进行复杂的格式化。
回答by Lars Nordin
Use lxml.builder class, from: http://lxml.de/tutorial.html#the-e-factory
使用 lxml.builder 类,来自:http://lxml.de/tutorial.html#the-e-factory
import lxml.builder as lb
from lxml import etree
nstext = "new story"
story = lb.E.Asset(
lb.E.Attribute(nstext, name="Name", act="set"),
lb.E.Relation(lb.E.Asset(idref="Scope:767"),
name="Scope", act="set")
)
print 'story:\n', etree.tostring(story, pretty_print=True)
Output:
输出:
story:
<Asset>
<Attribute name="Name" act="set">new story</Attribute>
<Relation name="Scope" act="set">
<Asset idref="Scope:767"/>
</Relation>
</Asset>
回答by John Smith Optional
I would use the yattaglibrary. I think it's the most pythonic way:
我会使用yattag库。我认为这是最pythonic的方式:
from yattag import Doc
doc, tag, text = Doc().tagtext()
with tag('food'):
with tag('name'):
text('French Breakfast')
with tag('price', currency='USD'):
text('6.95')
with tag('ingredients'):
for ingredient in ('baguettes', 'jam', 'butter', 'croissants'):
with tag('ingredient'):
text(ingredient)
print(doc.getvalue())
回答by SergO
An optional way if you want to use pure Python:
如果你想使用纯 Python,一种可选的方式:
ElementTreeis good for most cases, but it can't CDataand pretty print.
ElementTree适用于大多数情况,但它不能CData和漂亮的 print。
So, if you need CDataand pretty printyou should use minidom:
所以,如果你需要CData和漂亮的打印,你应该使用minidom:
minidom_example.py:
minidom_example.py:
from xml.dom import minidom
doc = minidom.Document()
root = doc.createElement('root')
doc.appendChild(root)
leaf = doc.createElement('leaf')
text = doc.createTextNode('Text element with attributes')
leaf.appendChild(text)
leaf.setAttribute('color', 'white')
root.appendChild(leaf)
leaf_cdata = doc.createElement('leaf_cdata')
cdata = doc.createCDATASection('<em>CData</em> can contain <strong>HTML tags</strong> without encoding')
leaf_cdata.appendChild(cdata)
root.appendChild(leaf_cdata)
branch = doc.createElement('branch')
branch.appendChild(leaf.cloneNode(True))
root.appendChild(branch)
mixed = doc.createElement('mixed')
mixed_leaf = leaf.cloneNode(True)
mixed_leaf.setAttribute('color', 'black')
mixed_leaf.setAttribute('state', 'modified')
mixed.appendChild(mixed_leaf)
mixed_text = doc.createTextNode('Do not use mixed elements if it possible.')
mixed.appendChild(mixed_text)
root.appendChild(mixed)
xml_str = doc.toprettyxml(indent=" ")
with open("minidom_example.xml", "w") as f:
f.write(xml_str)
minidom_example.xml:
minidom_example.xml:
<?xml version="1.0" ?>
<root>
<leaf color="white">Text element with attributes</leaf>
<leaf_cdata>
<![CDATA[<em>CData</em> can contain <strong>HTML tags</strong> without encoding]]> </leaf_cdata>
<branch>
<leaf color="white">Text element with attributes</leaf>
</branch>
<mixed>
<leaf color="black" state="modified">Text element with attributes</leaf>
Do not use mixed elements if it possible.
</mixed>
</root>
回答by Boa
I've tried a some of the solutions in this thread, and unfortunately, I found some of them to be cumbersome (i.e. requiring excessive effort when doing something non-trivial) and inelegant. Consequently, I thought I'd throw my preferred solution, web2py HTML helper objects, into the mix.
我已经尝试了该线程中的一些解决方案,但不幸的是,我发现其中一些解决方案很麻烦(即在做一些重要的事情时需要付出过多的努力)并且不够优雅。因此,我想我会将我的首选解决方案web2py HTML helper objects 混为一谈。
First, install the the standalone web2py module:
首先,安装独立的 web2py 模块:
pip install web2py
Unfortunately, the above installs an extremely antiquated version of web2py, but it'll be good enough for this example. The updated source is here.
不幸的是,上面安装了一个非常陈旧的 web2py 版本,但对于这个例子来说已经足够了。更新的来源在这里。
Import web2py HTML helper objects documented here.
导入此处记录的 web2py HTML 帮助程序对象。
from gluon.html import *
Now, you can use web2py helpers to generate XML/HTML.
现在,您可以使用 web2py 助手来生成 XML/HTML。
words = ['this', 'is', 'my', 'item', 'list']
# helper function
create_item = lambda idx, word: LI(word, _id = 'item_%s' % idx, _class = 'item')
# create the HTML
items = [create_item(idx, word) for idx,word in enumerate(words)]
ul = UL(items, _id = 'my_item_list', _class = 'item_list')
my_div = DIV(ul, _class = 'container')
>>> my_div
<gluon.html.DIV object at 0x00000000039DEAC8>
>>> my_div.xml()
# I added the line breaks for clarity
<div class="container">
<ul class="item_list" id="my_item_list">
<li class="item" id="item_0">this</li>
<li class="item" id="item_1">is</li>
<li class="item" id="item_2">my</li>
<li class="item" id="item_3">item</li>
<li class="item" id="item_4">list</li>
</ul>
</div>

