I’m using python and trying to take some XML and turn it into a dict. The code works fine except that some weird text is being added to the element tags which is then being added to the dict property names. This text seems to be the value of the “WebServiceGeocodeQueryResult” attribute: “xmlns”.
My code looks something like this:
import xml.etree.ElementTree as ET
import xml_to_dictionary # This is some code I found, it seems to work fine:
# http://code.activestate.com/recipes/410469-xml-as-dictionary/
def doSomeStuff()
theXML = """
<?xml version="1.0" encoding="utf-8"?>
<WebServiceGeocodeQueryResult
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="https://webgis.usc.edu/">
<TransactionId>7307e84c-d0c8-4aa8-9b83-8ab4515db9cb</TransactionId>
<Latitude>38.8092475915888</Latitude>
<Longitude>-77.2378689948621</Longitude>
...
"""
tree = ET.XML(result.content) # this is where the element names get the added '{https://webgis.usc.edu/}'
xmldict = xml_to_dictionary.XmlDictConfig(tree)
As you can see in the debugger, the element names in the object “tree” have the annoying prefix: “{https://webgis.usc.edu/}”:

And this prefix is translated to the dict property names:

That "weird text" is the namespace of the element. ElementTree expands element names to universal names.
You can preprocess your element names like this:
As an aside, if
cElementTreeis available, you should use that as it will be faster. (import xml.etree.cElementTree as ET)