Parsing XML with namespaces using ElementTree in Python

后端 未结 2 1020
眼角桃花
眼角桃花 2020-12-18 15:39

I have an xml, small part of it looks like this:



           


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-18 16:44

    This snippet from your question,

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

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

    This slightly modified code,

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

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

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


    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.

提交回复
热议问题