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