Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML library similar to simplejson/json? - Python

is there a similar library to simplejson, which would enable quick serialization of data to and from XML.

e.g. json.loads('{vol:'III', title:'Magical Unicorn'}')

e.g. json.dumps([1,2,3,4,5])

Any ideas?

like image 563
RadiantHex Avatar asked Jun 08 '10 10:06

RadiantHex


2 Answers

You're not going to find anything for xml as consistent as json, because xml doesn't know about data types. It depends on you to follow conventions or enforce adherence to an xml schema file.

That being said, if you're willing to accept the XML-RPC data structure mapping and a few limitations, check out the xmlrpclib package that lives in the Python standard library:

http://docs.python.org/library/xmlrpclib.html#convenience-functions

>>> import xmlrpclib
>>> s = xmlrpclib.dumps( ({'vol':'III', 'title':'Magical Unicorn'},))
>>> print s
<params>
<param>
<value><struct>
<member>
<name>vol</name>
<value><string>III</string></value>
</member>
<member>
<name>title</name>
<value><string>Magical Unicorn</string></value>
</member>
</struct></value>
</param>
</params>

>>> xmlrpclib.loads(s)[0]
({'vol': 'III', 'title': 'Magical Unicorn'},)
>>> 
like image 135
ʇsәɹoɈ Avatar answered Oct 06 '22 00:10

ʇsәɹoɈ


You can look how they have done it in Django: xml_serializer.py and tailor this to your needs.

like image 20
Etienne Avatar answered Oct 06 '22 01:10

Etienne