Skip to content
Advertisement

Converting xml to dictionary using ElementTree

I’m looking for an XML to dictionary parser using ElementTree, I already found some but they are excluding the attributes, and in my case I have a lot of attributes.

Advertisement

Answer

JavaScript

Call as

JavaScript

This works as long as you don’t actually have an attribute text; if you do, then change the third line in the function body to use a different key. Also, you can’t handle mixed content with this.

(Tested on LXML.)

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement