This function generates XML for any Python object through recursive functions. It is easy to add specific handlers for your own object types for more complex output options.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | def getXML(obj, objname=None):
"""getXML(obj, objname=None)
returns an object as XML where Python object names are the tags.
>>> u={'UserID':10,'Name':'Mark','Group':['Admin','Webmaster']}
>>> getXML(u,'User')
'<User><UserID>10</UserID><Name>Mark</Name><Group>Admin</Group><Group>Webmaster</Group></User>'
"""
if obj == None:
return ""
if not objname:
objname = "Deepdesk"
adapt={
dict: getXML_dict,
list: getXML_list,
tuple: getXML_list,
}
if adapt.has_key(obj.__class__):
return adapt[obj.__class__](obj, objname)
else:
return "<%(n)s>%(o)s</%(n)s>"%{'n':objname,'o':str(obj)}
def getXML_dict(indict, objname=None):
h = "<%s>"%objname
for k, v in indict.items():
h += getXML(v, k)
h += "</%s>"%objname
return h
def getXML_list(inlist, objname=None):
h = ""
for i in inlist:
h += getXML(i, objname)
return h
|
To support your own object types just create a getXML_myobj function and add the object type and function to adapt.
The function should take two arguments. First is the object to be converted and the second is the objname which should default to None.
The function should return a string with the XML for that object. getXML (or equivalent) should be called for any sub-objects as per the getXML_dict and getXML_list code.
This is a cut down version (my custom object handlers removed) of code I am using for a forthcoming project called Deepdesk.
Some Tweaks. I added some tweaks for formatting, CDATA, and compactness:
On line 4 of the toXML function, there's a "adapt.has_key" ... I've searched, but can't figure out what this is supposed to be... What is that part of?