我刚刚发现了lxml.objectify,它看起来很好读/写简单的XML文件。
首先,使用lxml.objectify是个好主意吗?例如,它是否已经成熟,是否仍在开发,并有可能在今后提供?
其次,如何防止objectify在下面的输出中添加像xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str"这样的标记?
输入: config.xml
<?xml version="1.0" encoding="utf-8"?>
<Test>
<MyElement1>sdfsdfdsfd</MyElement1>
</Test>码
from lxml import etree, objectify
with open('config.xml') as f:
xml = f.read()
root = objectify.fromstring(xml)
root.Information = 'maybe'
print etree.tostring(root, pretty_print=True)输出
<Test>
<MyElement1>sdfsdfdsfd</MyElement1>
<Information xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">maybe</Information>
</Test>发布于 2014-02-14 09:51:54
正如这里指出的:When using lxml, can the XML be rendered without namespace attributes?,这足以防止这个xmlns标记出现:
objectify.deannotate(root, xsi_nil=True)
etree.cleanup_namespaces(root)https://stackoverflow.com/questions/21767300
复制相似问题