serialize a class to XML and back

dieter dieter at handshake.de
Sat May 25 01:54:54 EDT 2013


Schneider <js at globe.de> writes:

> how can I serialize a python class to XML? Plus a way to get the class
> back from the XML?
>
> My aim is to store instances of this class in a database.

In case you want to describe the XML data via an XML-schema
(e.g. to exchange it with other applications; maybe via
WebServices), you may have a look at "PyXB".


The approach of "PyXB" may be a bit different from yours:

  It starts with an XML-schema description and from
  it generates Python classes corresponding to the types
  mentioned in the schema.

  Instances of those classes can then be easily serialized
  to XML and XML documents corresponding to types defined
  in the schema can easily be converted into corresponding
  class instances.

  It is not too difficult to customize the classes
  used for a given type - e.g. to give them special methods
  related to your application.


You may want to start with your (arbitrary) Python classes
and get their instances serialized into an adequate XML document.

This will not work in all cases: some things are very difficult
to serialize (maybe even not serializable at all - e.g. locks).

If you plan to use anything already existing, then almost
surely, this will impose restrictions of your classes.




More information about the Python-list mailing list