Using pylons - colander for xml deserialization

67 views
Skip to first unread message

saurabh prakash

unread,
Aug 21, 2012, 11:37:01 AM8/21/12
to pylons...@googlegroups.com
How should colander(http://docs.pylonsproject.org/projects/colander/en/latest/?awesome) be used for xml deserialization? Docs say that it can even be used for xml deserialization and validation, but I didn't find any good examples for that in docs or on the web!

If anyone has used colander for xml deserialization can you just put a snippet here on its usage? It would be very helpful.

Tres Seaver

unread,
Aug 22, 2012, 12:27:24 PM8/22/12
to pylons...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 08/21/2012 11:37 AM, saurabh prakash wrote:

> How should colander
> (http://docs.pylonsproject.org/projects/colander/en/latest/?awesome)
> be used for xml deserialization? Docs say that it can even be used for
> xml deserialization and validation, but I didn't find any good
> examples for that in docs or on the web!

colander's job in deserialization is to validate the "cstruct" (a Python
data structure extracted from whatever format, usually consisting of
sequences or mappings of strings) against a schema, converting it (if
validation succeeds) into an "appstruct" (a loosely equivalent data
structure, but with values massaged into their "canonical" form.

> If anyone has used colander for xml deserialization can you just put
> a snippet here on its usage? It would be very helpful.

E.g., for the example here:
http://docs.pylonsproject.org/projects/colander/en/latest/basics.html#deserializing-a-valid-serialization
a corresponding XML document might be::

<person>
<name>keith</name>
<age>20</age>
<friends>
<friend>
<id>1</id>
<name>jim<name>
</friend>
<friend>
<id>1</id>
<name>jim<name>
</friend>
<friend>
<id>1</id>
<name>jim<name>
</friend>
<friend>
<id>1</id>
<name>jim<name>
</friend>
</friends>
<phones>
<phone>
<location>home</location>
<number>555-1212</number>
</phone>
<phone>
<location>work</location>
<number>555-8989</number>
</phone>
</phones>
<person>

Your code would be responsible for converting the XML document into the
'cstruct' (which the linked example generates imperatively), before
passing the 'cstruct' to the 'schema.deserialize' method, from which it
would get back the 'appstruct'. You are free to use whatever tools you
like for parsing the XML and constructing the appstruct. I would be
likely to use lxml.

Hope this helps,


Tres.
- --
===================================================================
Tres Seaver +1 540-429-0999 tse...@palladion.com
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlA1CGUACgkQ+gerLs4ltQ4FiQCcDQ6Iz/Qt6PG9PFkNrBcu8tLY
N9wAnRD/mGoSvXyMWy3K/3Rmk09nrwSW
=fT/7
-----END PGP SIGNATURE-----

Reply all
Reply to author
Forward
0 new messages