Parsing XML with namespace in Python via 'ElementTree'

前端 未结 6 1701
臣服心动
臣服心动 2020-11-21 09:48

I have the following XML which I want to parse using Python\'s ElementTree:



        
6条回答
  •  青春惊慌失措
    2020-11-21 10:23

    My solution is based on @Martijn Pieters' comment:

    register_namespace only influences serialisation, not search.

    So the trick here is to use different dictionaries for serialization and for searching.

    namespaces = {
        '': 'http://www.example.com/default-schema',
        'spec': 'http://www.example.com/specialized-schema',
    }
    

    Now, register all namespaces for parsing and writing:

    for name, value in namespaces.iteritems():
        ET.register_namespace(name, value)
    

    For searching (find(), findall(), iterfind()) we need a non-empty prefix. Pass these functions a modified dictionary (here I modify the original dictionary, but this must be made only after the namespaces are registered).

    self.namespaces['default'] = self.namespaces['']
    

    Now, the functions from the find() family can be used with the default prefix:

    print root.find('default:myelem', namespaces)
    

    but

    tree.write(destination)
    

    does not use any prefixes for elements in the default namespace.

提交回复
热议问题