如何在Python中使用命名空间生成XML文档?
当前,您无法直接将命名空间添加到XML文档,因为内置的Pythonxml包尚不支持命名空间。因此,您需要将命名空间添加为标记的常规属性。例如,
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"/>
热门推荐
10 小侄过生日祝福语简短
11 小店营业的祝福语简短
12 担任老师的祝福语简短
13 饮料吧台祝福语简短大气
14 公司新春搞笑祝福语简短
15 高情商敬酒祝福语简短
16 婚礼祝福语新郎妹妹简短
17 教师答辩通过祝福语简短
18 生女孩红包简短祝福语