如何用 Python 生成本带有命名空间的 XML 文档?
目前,您不能直接向 XML 文档中添加命名空间,因为它尚未在内置 Python 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"/>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP