xml.etree.ElementTree - 设置xmlns = '...'时遇到问题
我好像漏掉了什么。我正在尝试设置一个谷歌产品数据源,但在注册命名空间时遇到了困难。
举个例子:
这里有说明:https://support.google.com/merchants/answer/160589
我想插入:
<rss version="2.0"
xmlns:g="http://base.google.com/ns/1.0">
这是代码:
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
tree = ElementTree
tree.register_namespace('xmlns:g', 'http://base.google.com/ns/1.0')
top = tree.Element('top')
#... more code and stuff
代码运行后,一切看起来都没问题,但我们缺少了 xmlns=
这个部分。
我发现用php创建XML文档更简单,但我想试试这个方法。我哪里出错了呢?
顺便问一下,或许在python中有更合适的方法,不用etree?
1 个回答
6
ElementTree的API文档在处理命名空间方面并没有解释得很清楚,但其实大致上还是比较简单的。你需要把元素用QName()
包裹起来,并且在你的命名空间参数中不要放xmlns
。
# Deal with confusion between ElementTree module and class name
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ElementTree, Element, SubElement, QName, tostring
from io import BytesIO
# Factored into a constant
GOOG = 'http://base.google.com/ns/1.0'
ET.register_namespace('g', GOOG)
# Make some elements
top = Element('top')
foo = SubElement(top, QName(GOOG, 'foo'))
# This is an alternate, seemingly inferior approach
# Documented here: http://effbot.org/zone/element-qnames.htm
# But not in the Python.org API docs as far as I can tell
bar = SubElement(top, '{http://base.google.com/ns/1.0}bar')
# Now it should output the namespace
print(tostring(top))
# But ElementTree.write() is the one that adds the xml declaration also
output = BytesIO()
ElementTree(top).write(output, xml_declaration=True)
print(output.getvalue())