且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Python中的ElementTree解析具有名称空间的XML

更新时间:2021-08-30 04:09:35

此片段来自您的问题,

for a in root.findall('{urn:com:xml:data}image'):
    print a.attrib

不输出任何内容,因为它只查找直接的 {urn:com:xml:data}图像树根的子代。

does not output anything because it only looks for direct {urn:com:xml:data}image children of the root of the tree.

此代码稍作修改,

for a in root.findall('.//{urn:com:xml:data}image'):
    print a.attrib

将打印 {'imageId':'1'} ,因为它使用的是。/ / ,它会在所有级别上选择匹配的子元素。

will print {'imageId': '1'} because it uses .//, which selects matching subelements on all levels.

参考: https://docs.python.org/2/library/xml.etree.elementtree.html#supported-xpath-syntax

令人讨厌的是,ElementTree不仅保留了默认情况下是原始名称空间前缀,但请记住,无论如何前缀都不重要。 register_namespace()函数可用于在对XML进行序列化时设置所需的前缀。该功能对解析或搜索没有任何影响。

It is a bit annoying that ElementTree does not just retain the original namespace prefixes by default, but keep in mind that it is not the prefixes that matter anyway. The register_namespace() function can be used to set the wanted prefix when serializing the XML. The function does not have any effect on parsing or searching.