从Python数据结构生成XML
xml-from-seq的Python项目详细描述
xml来自
从Python数据结构生成XML。在
示例
XML()
函数将Python列表或元组呈现为XML元素。第一项是
元素名,第二项(如果是dict)是属性,其余的项
是文本或嵌套元素。在
fromxml_from_seqimportXMLitem=['item',{'attr1':123,'attr2':'other value'},'This is the content of the item.']assertXML(item)=='<item attr1="123" attr2="other value">This is the content of the item.</item>'item=['item','This is some content of the item.'['sub','This is the content of a subelement.']]print(XML(item))^{pr2}$
如果一个元素的名称是None
,它的内容将出现在它的位置上。如果属性的值为
None
它将被省略。在
如果元素的名称是一个列表或元组,它将按原样插入到XML中,这样您就可以包含 已经通过双括号呈现了XML:
print(XML([['<foo>123</foo>']]))
<foo>123</foo>
缩进和换行
如果元素中的第一项(不计算属性dict)是xml_from_seq.INLINE
,则
元素的内容不会从元素的开始和结束标记的单独行上缩进。在
fromxml_from_seqimportINLINE,XMLitem=['item','This is some content of the item.'['sub',INLINE,'This is the content of a subelement.']]print(XML(item))
<item> This is some content of the item. <sub>This is the content of a subelement.</sub></item>
您可以将一个整型indent
参数传递给XML()
函数,以通过
那么多标签。在
- 项目
标签: