I would advise taking a look at declxml to see if it fits your use case (full disclosure: I'm the author). With declxml, you create objects called processors that declaratively define the structure of your XML document. Processors are used to parse and serialize between XML and Python values, including objects, dictionaries, and named elements.
import declxml as xml some_xml = """ <mydocument has="an attribute"> <and> <many>elements</many> <many>more elements</many> </and> <plus a="complex"> element as well </plus> </mydocument> """ processor = xml.dictionary('mydocument', [ xml.string('.', attribute='has'), xml.array(xml.string('many'), nested='and'), xml.dictionary('plus', [ xml.string('.', attribute='a'), xml.string('.', alias='plus') ]) ]) xml.parse_from_string(processor, some_xml)
What makes the following conclusion
{'has': 'an attribute', 'and': ['elements', 'more elements'], 'plus': {'a': 'complex', 'plus': 'element as well'}}
source share