Python has a json library where you can do json.loads(s) to load json into a python data structure consisting of lists and maps. Is there something equivalent for xml? All the xml libraries I've seen map to a DOM where you have to know special API functions, which is IMO less easy to work with.
-
1That would require XML to have a reasonable mapping into Python core types. It doesn't.Amber– Amber2012-05-01 22:13:46 +00:00Commented May 1, 2012 at 22:13
Add a comment
|
1 Answer
No, on the simple note that JSON objects directly translate to Python objects. How do you turn XML into Python - the answer is the DOM.
There are, however, simpler ways to work with XML. The best, in my opinion, for ease of use, is xml.etree.ElementTree, which allows you to iterate through the elements of the XML and create a tree, which is pretty easy to work with comparable with other ways of getting at XML data.
3 Comments
Charles Duffy
I'd generally agree with this, but note that lxml.etree (see lxml.de) is well worthwhile -- having true XPath support, XSLT, and many other goodies while retaining etree's ease-of-use.
Gareth Latty
@CharlesDuffy Indeed, if you need that functionality,
lxml is a great library - for simpler uses, I generally find the standard library suffices.Kevin
I ended up using xml.etree.ElementTree. Thanks.