如何在Python中生成带命名空间的XML文档?

如何在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 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程