I am looking to convert a Python object into XML data. I\'ve tried lxml, but eventually had to write custom code for saving my object as xml which isn\'t perfect.
I\
Mike,
you can either implement object rendering into XML :
class Person:
...
def toXml( self):
print '<Person>'
print '\t<name>...</name>
...
print '</Person>'
or you can transform Gnosis or pyxser output using XSLT.
Best is rather subjective and I'm not sure it's possible to say what's best without knowing more about your requirements. However Gnosis has previously been recommended for serializing Python objects to XML so you might want to start with that.
From the Gnosis homepage:
Gnosis Utils contains several Python modules for XML processing, plus other generally useful tools:
- xml.pickle (serializes objects to/from XML)
- API compatible with the standard pickle module)
- xml.objectify (turns arbitrary XML documents into Python objects)
- xml.validity (enforces XML validity constraints via DTD or Schema)
- xml.indexer (full text indexing/searching)
- many more...
Another option is lxml.objectify.