0

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.

1
  • 1
    That would require XML to have a reasonable mapping into Python core types. It doesn't. Commented May 1, 2012 at 22:13

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@CharlesDuffy Indeed, if you need that functionality, lxml is a great library - for simpler uses, I generally find the standard library suffices.
I ended up using xml.etree.ElementTree. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.