如何在Python中生成带命名空间的XML文档?
目前,在Python的内置XML包中还不支持直接添加命名空间到XML文档中。因此,您需要将命名空间添加为普通的属性标签。例如,
import xml.dom.minidom
doc = xml.dom.minidom.Document()
element = doc.createElementNS('http://hello.world/ns', 'ex:el')
element.setAttribute("xmlns:ex", "http://hello.world/ns")
doc.appendChild(element)
print(doc.toprettyxml())
这将给您生成以下文档,
<?xml version="1.0" ?>
<ex:el xmlns:ex="http://example.net/ns"/>
阅读更多:Python 教程
极客教程