Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

serialize a class to XML and back

919 views
Skip to first unread message

Schneider

unread,
May 23, 2013, 6:37:05 AM5/23/13
to pytho...@python.org
Hi list,

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.

bg,
Johannes

--
GLOBE Development GmbH
Königsberger Strasse 260
48157 MünsterGLOBE Development GmbH
Königsberger Strasse 260
48157 Münster
0251/5205 390

Burak Arslan

unread,
May 23, 2013, 7:16:34 AM5/23/13
to Schneider, pytho...@python.org
On 05/23/13 13:37, Schneider wrote:
> Hi list,
>
> 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.

Hi,

I'm working on a project called Spyne (http://spyne.io). With one object
definition, you can serialize to/from xml and save to database via
SQLAlchemy.

The code generator on the site has examples for RPC and SQL. If you're
not interested in doing RPC with the resulting document, here's an
example for just the xml part:
https://github.com/arskom/spyne/blob/master/examples/xml_utils.py

Best regards,
Burak

alex23

unread,
May 23, 2013, 8:01:01 PM5/23/13
to
On May 23, 8:37 pm, Schneider <j...@globe.de> wrote:
> My aim is to store instances of this class in a database.

Have you considered just pickling the classes instead?

dieter

unread,
May 25, 2013, 1:54:54 AM5/25/13
to pytho...@python.org
Schneider <j...@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.

Chris Rebert

unread,
May 26, 2013, 4:36:53 PM5/26/13
to Schneider, Python

On May 23, 2013 3:42 AM, "Schneider" <j...@globe.de> wrote:
>
> Hi list,


>
> how can I serialize a python class to XML? Plus a way to get the class back from the XML?

There's pyxser: http://pythonhosted.org/pyxser/

> My aim is to store instances of this class in a database.

Honestly, I would avoid XML if you can. Consider using JSON (Python includes the `json` module in the std lib) or pickle instead. Compared to XML: The former is more standardized (in the context of serializing objects) and less verbose; the latter is more efficient (if you don't care about cross-language accessibility); both have more convenient APIs.

Cheers,
Chris

Roy Smith

unread,
May 26, 2013, 4:48:21 PM5/26/13
to
In article <mailman.2197.1369600...@python.org>,
Some other points...

If you care about efficiency and want to use json, don't use the one
that comes packaged with the standard library. There are lots of
third-party json packages (ujson is the one we use) which are
significantly faster. Not sure if that's true of the newest python
releases, but it was certainly true in 2.6.

The advantage of pickle over json is that pickle can serialize many
types of objects that json can't. The other side of the coin is that
pickle is python-specific, so if you think you'll ever need to read your
data from other languages, pickle is right out.

Irmen de Jong

unread,
May 26, 2013, 6:40:01 PM5/26/13
to
On 26-5-2013 22:48, Roy Smith wrote:

> The advantage of pickle over json is that pickle can serialize many
> types of objects that json can't. The other side of the coin is that
> pickle is python-specific, so if you think you'll ever need to read your
> data from other languages, pickle is right out.

That is not entirely true :) I've written a pickle implementation for Java and .NET
that is almost feature complete; it is part of http://pythonhosted.org/Pyro4/pyrolite.html

Still, pickle may not be the best choice here.

Cheers
Irmen

Roy Smith

unread,
May 26, 2013, 8:39:01 PM5/26/13
to
In article <51a28f42$0$15870$e4fe...@news.xs4all.nl>,
Irmen de Jong <irmen....@xs4all.nl> wrote:

> On 26-5-2013 22:48, Roy Smith wrote:
>
> > The advantage of pickle over json is that pickle can serialize many
> > types of objects that json can't. The other side of the coin is that
> > pickle is python-specific, so if you think you'll ever need to read your
> > data from other languages, pickle is right out.
>
> That is not entirely true :) I've written a pickle implementation for Java
> and .NET
> that is almost feature complete; it is part of
> http://pythonhosted.org/Pyro4/pyrolite.html

Very cool

> Still, pickle may not be the best choice here.

Perhaps not, but lots of points for the awesomeness factor.

Irmen de Jong

unread,
May 27, 2013, 2:45:07 PM5/27/13
to
Thanks for the praise :)

There's another interesting thing perhaps to also mention about Pyrolite. Its Pickle
implementation obviously maps built in types to their counterparts in Java/.NET, but it
is also capable of unpickling custom classes. It defaults to outputting a simple
hashtable with the class's properties in it, but you can also provide a custom
deserializer to recreate a custom object (it already does this automatically for a few
complex types such as DateTime, BigDecimal, and a bunch of Pyro specific types).

As the unpicklers are not executing any Java or .NET code dynamically they are not
susceptible to the security issue that Python's pickle has. Still: tread with care.
(Especially if you use the lib to pickle stuff and unpickle it on the Python side).

Also, the one missing feature is memo-ing when pickling so that you can pickle recursive
object graphs. It now throws a stack overflow exception instead.


Irmen


Schneider

unread,
May 31, 2013, 7:21:39 AM5/31/13
to pytho...@python.org
On 26.05.2013 22:48, Roy Smith wrote:
> In article <mailman.2197.1369600...@python.org>,
> Chris Rebert <cl...@rebertia.com> wrote:
>
>> On May 23, 2013 3:42 AM, "Schneider" <j...@globe.de> wrote:
>>> Hi list,
>>>
>>> how can I serialize a python class to XML? Plus a way to get the class
>> back from the XML?
>>
>> There's pyxser: http://pythonhosted.org/pyxser/
>>
>>> My aim is to store instances of this class in a database.
>> Honestly, I would avoid XML if you can. Consider using JSON (Python
>> includes the `json` module in the std lib) or pickle instead. Compared to
>> XML: The former is more standardized (in the context of serializing
>> objects) and less verbose; the latter is more efficient (if you don't care
>> about cross-language accessibility); both have more convenient APIs.
> Some other points...
>
> If you care about efficiency and want to use json, don't use the one
> that comes packaged with the standard library. There are lots of
> third-party json packages (ujson is the one we use) which are
> significantly faster. Not sure if that's true of the newest python
> releases, but it was certainly true in 2.6.

I think performance can be a problem in future. This question is part of
a multi-user rss-reader solution, which I'm going to develop.

I want to store the feed entries (+ some additional data) as XML in a
database.

> The advantage of pickle over json is that pickle can serialize many
> types of objects that json can't. The other side of the coin is that
> pickle is python-specific, so if you think you'll ever need to read your
> data from other languages, pickle is right out.


Schneider

unread,
May 31, 2013, 7:16:21 AM5/31/13
to dieter, pytho...@python.org
On 25.05.2013 07:54, dieter wrote:
> Schneider <j...@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).

I have just small classes containing text (strings) numbers (as ids)
and references to other classes of this type.

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


0 new messages