Parsing XML with namespaces using ElementTree in Python

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

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



           


        
2条回答
  •  隐瞒了意图╮
    2020-12-18 16:27

    From what I gather, it has something to do with the namespace recognition in ET.

    from here http://effbot.org/zone/element-namespaces.htm

    When you save an Element tree to XML, the standard Element serializer generates unique prefixes for all URI:s that appear in the tree. The prefixes usually have the form “ns” followed by a number. For example, the above elements might be serialized with the prefix ns0 for “http://www.w3.org/1999/xhtml” and ns1 for “http://effbot.org/namespace/letters”.

    If you want to use specific prefixes, you can add prefix/uri mappings to a global table in the ElementTree module. In 1.3 and later, you do this by calling the register_namespace function. In earlier versions, you can access the internal table directly:

    ElementTree 1.3

    ET.register_namespace(prefix, uri)

    ElementTree 1.2 (Python 2.5)

    ET._namespace_map[uri] = prefix

    Note the argument order; the function takes the prefix first, while the raw dictionary maps from URI:s to prefixes.

提交回复
热议问题