生成xml的最佳方法是什么?

时间:2010-10-02 04:14:11

标签: python xml api

我正在创建一个web api,需要一个很好的方法来快速生成一些格式良好的xml。我在python中找不到任何好的方法。

注意:有些库看起来很有前途,但要么缺少文档,要么只输出到文件。

6 个答案:

答案 0 :(得分:87)

使用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>

有关详细信息,请参阅tutorial

答案 1 :(得分:85)

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)

输出:

<root><child>I am a child</child></root>

有关详细信息以及如何打印,请参阅此tutorial

或者,如果您的XML很简单,请不要低估字符串格式的强大功能:)

xmlTemplate = """<root>
    <person>
        <name>%(name)s</name>
        <address>%(address)s</address>
     </person>
</root>"""

data = {'name':'anurag', 'address':'Pune, india'}
print xmlTemplate%data

输出:

<root>
    <person>
        <name>anurag</name>
        <address>Pune, india</address>
     </person>
</root>

您也可以使用string.Template或某些模板引擎进行复杂格式化。

答案 2 :(得分:15)

我会使用yattag库。我认为这是最蟒蛇的方式:

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())

答案 3 :(得分:14)

使用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)

输出:

story:
<Asset>
  <Attribute name="Name" act="set">new story</Attribute>
  <Relation name="Scope" act="set">
    <Asset idref="Scope:767"/>
  </Relation>
</Asset>

答案 4 :(得分:9)

如果你想使用纯Python,可选择一种方法:

ElementTree 适用于大多数情况,但它不能 CData 漂亮的打印

因此,如果您需要 CData 漂亮的打印,您应该使用minidom

<强> 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:

<?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>

答案 5 :(得分:0)

我在这个帖子中尝试了一些解决方案,不幸的是,我发现其中一些是繁琐的(即在做一些非平凡的事情时需要过多的努力)并且不优雅。因此,我认为我会将我的首选解决方案web2py HTML helper objects投入到混合中。

首先,安装standalone web2py module

pip install web2py

不幸的是,上面安装了一个非常陈旧的web2py版本,但对于这个例子来说它已经足够了。更新的来源为here

导入web2py HTML帮助程序对象,记录为here

from gluon.html import *

现在,您可以使用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>