从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()函数,以通过 那么多标签。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
具有parend子关系的java Save Hibernate实体失败,出现外键错误   数组如何从嵌套数组中收集结果。Java IntStream中的forEach   java替换正则表达式中的Group2   java匕首2:避免导出私有依赖项   java我怎样才能把方块放在播放器下面?   java Eclipse JDT自动格式化:不带大括号的“if”后面的缩进语句   lambda如何使用Java8流和过滤器过滤嵌套循环?   java JPA批插入不能提高性能   java时间转换为时间戳   java我添加数组奇数的方法的逻辑有什么问题   使用迭代器从Java集合中删除元素   java如何使用spring生成i18n错误   ubuntu如何在没有glassfish的情况下安装/下载JavaEE javadocs?   java一个接一个地显示SQLite列