0

I need to parse a XML file and create a equivalent Dictionary for the same.

I have gone through previous Stack Overflow question for the same.

Converting xml to dictionary using ElementTree

Now I have a XML content as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Data>
  <Person>
    <First>John</First>
    <Last>Smith</Last>
    <extra>
        <details1>
            <married>yes</married>
            <status>rich</status>
        </details1>
    </extra>
  </Person>
  <Person>
    <First>Jane</First>
    <Last>Doe</Last>
        <extra>
        <details1>
            <married>yes</married>
            <status>rich</status>
        </details1>
        <details2>
            <property>yes</property>
        </details2>
    </extra>
  </Person>
</Data>

It is returning the output as follows:

[
 {
  "extra": "\n\t\t",
  "Last": "Smith",
  "First": "John"
 }
]
[
 {
  "extra": "\n\t\t",
  "Last": "Smith",
  "First": "John"
 },
 {
  "extra": "\n\t\t",
  "Last": "Doe",
  "First": "Jane"
 }
]

So I need a way to parse the content of child element also and return the result please some one let me know the way for the same.

Current code:

import json
from lxml import etree
tree= etree.parse("dummy.xml")
root = tree.getroot()
datadict = []
for item in root:
    d = {}
    for elem in item:
        d[elem.tag]=elem.text
    datadict.append(d)
    new_d = json.dumps(datadict, indent=1)
    print new_d
Community
  • 1
  • 1
Murali
  • 1,084
  • 1
  • 11
  • 28
  • Don't really understand why you have a problem. You have referred to a question with two perfectly viable answers but aren't using their code. Those answers have a recursive decoder to handle children. Why aren't you using that? – DisappointedByUnaccountableMod Mar 28 '17 at 12:01

1 Answers1

0

Have a look at the docs. You might want to iterate over the children as well with something like this:

[ child.tag for child in item.iterchildren() ]

Remi Smirra
  • 2,499
  • 1
  • 14
  • 15