开发者

How to write XML elements with namespaces in python

开发者 https://www.devze.com 2022-12-20 16:32 出处:网络
I just tried this python 2.5 snippet to write some XML: xmldoc = xml.dom.minidom.Document() feed = xmldoc.createElementNS(\"http://www.w3.org/2005/Atom\", \"feed\")

I just tried this python 2.5 snippet to write some XML:

xmldoc = xml.dom.minidom.Document()
feed = xmldoc.createElementNS("http://www.w3.org/2005/Atom", "feed")
xmldo开发者_开发知识库c.appendChild(feed)
print xmldoc.toprettyxml()

I expected the output to look like this:

<?xml version="1.0" ?>
<feed xmlns="http://www.w3.org/2005/Atom" />

Instead I got this:

<?xml version="1.0" ?>
<feed />

Apparently the XML namespace is silently being ignored here. What am I doing wrong?


minidom does not support namespace normalization. The only workaround I'm aware of is explicitely setting the xmlns attribute with

xmldoc.documentElement.setAttribute('xmlns', 'http://www.w3.org/2005/Atom')
0

精彩评论

暂无评论...
验证码 换一张
取 消