从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第三方库


热门话题
java中的else if语句返回语法错误   http如何解析从表单到REST Java/Webservice的post请求   java如何在SpringBoot中为RestTemplate编写JUnit测试   java动态检查实例是否实现接口   java在Android中使用ArrayAdaptor时,数据不会显示   根据前序遍历返回二进制树的java方法   Arquillian测试类中的java注入始终为空   用户界面Java Swing自定义控件   java使用CompletableFutures递归地从同一函数的多个调用构建列表   在Java中将“.00”添加到整数BigDecimal   卡夫卡java未知\u主题\u或\u部分错误间歇性   java为什么我的getString()方法总是返回null?   java renameTo无法重命名文件   java为什么我可以强制转换对象?可以用其他对象来完成吗?   带有回写条件的java多根记录器   关于stackoverflow错误的java帮助?   java Websphere消息队列多线程   Java图形窗口/画布未垂直显示完整的六边形网格   java Sakai未在Tomcat服务器中启动