How to convert XML to Dict

I want to find a great solution for converting XML to dict and vice versa in python

+1
source share
5 answers

The following recipe should be helpful:

0
source

xmltodict (full disclosure: I wrote) does just that, following this "standard" . This is Expat , so it is very fast and does not require loading the entire XML tree into memory.

>>> print(json.dumps(xmltodict.parse(""" ... <mydocument has="an attribute"> ... <and> ... <many>elements</many> ... <many>more elements</many> ... </and> ... <plus a="complex"> ... element as well ... </plus> ... </mydocument> ... """), indent=4)) { "mydocument": { "@has": "an attribute", "and": { "many": [ "elements", "more elements" ] }, "plus": { "@a": "complex", "#text": "element as well" } } } 
+10
source

When converting between XML and Python dictionaries, there are some interesting angular cases that make this non-trivial (attribute lists "anonymous list lists" single lists? Content eval?): View this document from PicklingTools distribution: Converting XML to Dict : http: // www .picklingtools.com

The docs discuss how to do this, but here is a simple example. In the file named example.xml, we add the following XML:

 <top> <a>1</a> <b>2.2</b> <c>three</c> </top> 

To process this file and turn it into a dictionary:

 >>> from xmlloader import * >>> example = file('example.xml', 'r') >>> xl = StreamXMLLoader(example, 0) # 0 = All defaults on options >>> result = xl.expectXML() >>> print result {'top': {'a': '1', 'c': 'three', 'b': '2.2'}} 
+1
source

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'}} 
0
source

I think the best way is to make your own to fit your needs. Get lxml , read the docs, and you should be good to go. If in doubt, go back :)

-2
source

Source: https://habr.com/ru/post/947589/


All Articles