问题描述
是否可以忽略 elementtree.ElementTree
中的年龄名称中的XML名称空间?
Is there a way to ignore the XML namespace in tage names in elementtree.ElementTree
?
我尝试打印所有 technicalContact
标签:
for item in root.getiterator(tag='{http://www.example.com}technicalContact'):
print item.tag, item.text
我得到类似的东西:
{http://www.example.com}technicalContact [email protected]
但我真正想要的是:
technicalContact [email protected]
是否有办法仅显示后缀(没有xmlns),或者更好-在元素上进行迭代而不显式声明xmlns?
Is there a way to display only the suffix (sans xmlns), or better - iterate over the elements without explicitly stating xmlns?
推荐答案
您可以定义一个生成器以递归地搜索元素树,以找到以适当标签名称结尾的标签。例如,如下所示:
You can define a generator to recursively search through your element tree in order to find tags which end with the appropriate tag name. For example, something like this:
def get_element_by_tag(element, tag):
if element.tag.endswith(tag):
yield element
for child in element:
for g in get_element_by_tag(child, tag):
yield g
这只是检查以 tag
结尾的标签,即忽略任何领先的命名空间。然后,您可以迭代所需的任何标签,如下所示:
This just checks for tags which end with tag
, i.e. ignoring any leading namespace. You can then iterate over any tag you want as follows:
for item in get_element_by_tag(elemettree, 'technicalContact'):
...
此生成器正在运行:
>>> xml_str = """<root xmlns="http://www.example.com">
... <technicalContact>Test1</technicalContact>
... <technicalContact>Test2</technicalContact>
... </root>
... """
xml_etree = etree.fromstring(xml_str)
>>> for item in get_element_by_tag(xml_etree, 'technicalContact')
... print item.tag, item.text
...
{http://www.example.com}technicalContact Test1
{http://www.example.com}technicalContact Test2
这篇关于Python:忽略elementtree.ElementTree中的xmlns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!