I want to parse an XML content and return a dictionary which contains only the name attribute and its values as dictionary. For example:
<ecmaarray>
<number name="xyz1">123.456</number>
<ecmaarray name="xyz2">
<string name="str1">aaa</string>
<number name="num1">55</number>
</ecmaarray>
<strictarray name="xyz3">
<string>aaa</string>
<number>55</number>
</strictarray>
</ecmaarray>
The output has to be in a dictionary something like this..
Dict:{ 'xyz1': 123.456,
'xyz2': {'str1':'aaa', 'num1': '55'},
'xyz3': ['aaa','55']
}
Can any one suggest a recursive solution for this ?
xmltodict
is ideal for this use case. Though it likely won't generate quite that dictionary (by default, anyways).xml2dict
function will parse the xml to a Python dictionary in which elements are keys. You could then easily change the keys so that the dictionary has the format you desire.lxml
will allow you to write xpath expressions to extract what you need. You could also write a complete parser withstart_tag
andend_tag
functions, but I wouldn't recommend that over the other two suggestions. You should try using one of the above methods, then post back with your code if you have problems.