8
votes

I'm searching for an easy to handle python native module to create python object representation from xml.

I found several modules via google (one of them is XMLObject) but didn't want to try out all of them.

What do you think is the best way to do such things?

EDIT: I missed to mention that the XML I'd like to read is not generated by me. It's an existing XML file in a structure of which I have no control over.

7
Are you just looking to serialize an object? or is XML a hard requirement? - Jeremy Cantrell
I need to read existing XML files - so XML is a hard requirement - Martin

7 Answers

12
votes

You say you want an object representation, which I would interpret to mean that nodes become objects, and the attributes and children of the node are represented as attributes of the object (possibly according to some Schema). This is what XMLObject does, I believe.

There are some packages that I know of. 4Suite includes some tools to do this, and I believe Amara specifically implements this (built on top of 4Suite). You can also use lxml.objectify, which was inspired by Amara and gnosis.xml.objectify.

Of course a third option is, given a concrete representation of the XML (using ElementTree or lxml) you can build your own custom model around that. lxml.html is an example of that, extending the base interface of lxml with some HTML-specific functionality.

5
votes

I second the suggestion of xml.etree.ElementTree, mostly because it's now in the stdlib. There is also a faster implementation, xml.etree.cElementTree available too.

If you really need performance, I would suggest lxml

http://www.ibm.com/developerworks//xml/library/x-hiperfparse/

3
votes

I've heard the easiest is ElementTree, though I rarely work with XML and I can't say anything from experience.

3
votes

There's also the excellent 3rd party library pyxser for Python.

pyxser stands for Python XML Serialization and is a Python object to XML serializer and deserializer. In other words, it can convert a Python object into XML and also, convert that XML back into the original Python object.

1
votes

I use (and like) PyRXP, which creates a tuple built from the XML document.

The main issue with a straight XML -> python object structure is that there is no python analog for a attributed list - that is, a list with elements, that also happens to have attributes. If you like, it is both a list and a dictionary at the same time.

I parse the result from PyRXP, and create the list/dictionary depending upon the structure - the XML I am dealing with is either list or attribute-based, never both. (I am consuming data from a known source).

1
votes

Python has pickle and cPickle modules for Python object serialization. Both of these modules provide functionality to serialize/deserialize Python object hierarchy to convert to/from a byte stream:

The following provides similar interface: pickle(), unpickle() for serialization to/from XML

0
votes

I've had pretty good luck with Wai Yip Tung's xml2obj function available here:

http://code.activestate.com/recipes/534109-xml-to-python-data-structure/

It's ~84 lines of code. It's native and pure python; using xml.sax and re (regular expression) libraries. You just pass it XML and get back your object.