I use python 2.7 with the lib ElementTree.
I can\'t use lxml lib.
I need to get the namespaces in a string namespace_string. In order to fill my
The code for creating a dictionary with all the declared namespaces can be made quite simple. This is all that is needed:
import xml.etree.ElementTree as ET
my_namespaces = dict([node for _, node in ET.iterparse('file.xml',
events=['start-ns'])])
You don't need to use StringIO or open(). Just provide the XML filename as an argument to iterparse().
Each item provided by iterparse() is an (event, (prefix, namespace-uri)) tuple. The start-ns event is not described in the Python 2.7 documentation of iterparse (but it is mentioned in the corresponding Python 3 documentation).
Note: the code above works in CPython and Jython, but not in IronPython. See https://github.com/IronLanguages/main/issues/968.