snippets / simple xml serializer

Language: Python - First posted by benoitc on 2008-04-29 17:58 (5 months, 1 week)
Link to the snippet: http://www.friendsnippets.org/snippet/269/

simple xml serializer that convert a python object in xml file.

 1 def serialize_xml(d):
2 # simple serializer
3 if not isinstance(d, (dict,list,tuple)):
4 raise TypeError("Expected a dict here")
5 yield "\r\n"
6 if isinstance(d, dict):
7 for k,v in d.items():
8 if isinstance(v, (dict,list,tuple)):
9 v = "".join(serialize_xml(v))
10 yield "<%(key)s>%(value)s</%(key)s>\r\n" % {
11 'key':k,
12 'value':v
13 }
14 elif isinstance(d, (tuple, list)):
15 for v in d:
16 if isinstance(v, (dict,list,tuple)):
17 v = serialize_xml(v)
18 yield "<value>%s</value>\r\n" % v
In order to post a comment, you should have a friendsnippet account. Please sign-in.

0 comments

Apr '08
  • simple xml serializer that convert a python object in xml file.

Common Tags



snippet History

Apr '08