我有一个对象,它是列表类型的。print type(result)返回<type 'list'>。有没有自动转换成XML的函数?
发布于 2014-04-15 21:31:40
从lxml检查objectify和E-factory。
E = objectify.E
result = [{ 'a': 1 }, {'a': 2} , {'a': 3}]   
child_tags = [E.mytag(el) for el in result]
xml = E.root(*child_tags)
etree.tostring(xml, pretty_print=True)E为任何标签提供了构造函数。在我的示例中,列表中的每个元素都有标记mytag,每个标记都有对应于dict键的属性。因此,生成的xml将是:
<root xmlns:py="http://codespeak.net/lxml/objectify/pytype"  
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <mytag a="1"/>
  <mytag a="2"/>
  <mytag a="3"/>
</root>https://stackoverflow.com/questions/23085118
复制相似问题