0

I am parsing an XML file and I need to populate a nested dictionary where keys of inner dictionary points a list object. I need to append a new value to the inner list at every iteration.

Sample XML

<entry name = 'Europe'>
  <entry name = 'Spain'>
    <from>'Madrid'</from>
    <from>'Barcelona'</from>
  <entry name = 'Nederland'>
    <from>'Amsterdam'</from>
    <from>'Roterdam'</from>

I need to have such output from parsing

d = {'Europe':{'Spain':['Madrid','Barcelona']}, {'Nederland':['Amsterdam','Roterdam']}

I am using xml.etree.ElementTree to parse XML. However, couldn't populate such a dictionary above. The code I did

import xml.etree.ElementTree as ET
tree = ET.parse(europe.xml)
root = tree.getroot()
for i in root.findall('.//entry'):
   for j in root.findall(''.//entry/):
      l = []
      l_new = [:]
      for k in root.findall('.//entry/entry/):
         l_new.append(k.text)
         d1[j] = l_new
...

Any idea?

edmpg
  • 3
  • 1
  • 1
    Can you mention what you're getting with your current code? – magikarp Mar 19 '20 at 16:06
  • Also, `for j in root.findall(''.//entry/):` -> `for j in root.findall('.//entry/'):`. – magikarp Mar 19 '20 at 16:06
  • And `for k in root.findall('.//entry/entry/):` -> `for k in root.findall('.//entry/entry/'):`. – magikarp Mar 19 '20 at 16:07
  • It's typo the correct one should be for j in root.findall('.//entry/'): and for k in root.findall('.//entry/entry/'): – edmpg Mar 19 '20 at 16:10
  • I think you can refer to the answer of [Converting XML to JSON using Python?](https://stackoverflow.com/a/10201405/8711819) –  Mar 19 '20 at 16:24

1 Answers1

1

You can access the sub elements from each element in the xml.etree. To nest dictionaries, you'll need to add them as you go and work with the inner dictionaries when you are parsing the sub-elements.

europe="""<xml>
<entry name = 'Europe'>
  <entry name = 'Spain'>
    <from>'Madrid'</from>
    <from>'Barcelona'</from>
  </entry>
  <entry name = 'Nederland'>
    <from>'Amsterdam'</from>
    <from>'Roterdam'</from>
  </entry>
</entry>
</xml>"""

import xml.etree.ElementTree as ET
root = ET.fromstring(europe)
d = dict()
for rg in root.findall("entry"):
    # add the region level to the dictionary (e.g. "europe") 
    d[rg.attrib["name"]] = region = {} 
    for cn in rg.findall("entry"):
        # add the country to the current region level (e.g. "spain")
        region[cn.attrib["name"]] = country = []
        for city in cn.findall("from"):
            # add the city to the current region
            country.append(city.text) 

output:

print(d)

{'Europe':
    {'Spain': ["'Madrid'", "'Barcelona'"],
     'Nederland': ["'Amsterdam'", "'Roterdam'"]}}
Alain T.
  • 40,517
  • 4
  • 31
  • 51