[Answer]-Can Django Serializers serialize non Django classes/objects

1๐Ÿ‘

โœ…

I think I got it, provided you instantiate the class, Iโ€™m not sure you want to convert class or object:

import inspect
from xml.etree.ElementTree import Element, tostring


def obj_to_xml(obj):
    elem = Element(obj.__class__.__name__)
    for name, method in inspect.getmembers(obj, inspect.ismethod):
        if name in obj.__dict__.keys():
            child = Element(name)
            method()
            for gname in method.__code__.co_names: 
                gchild = Element(gname)
                gchild.text = str(obj.__dict__[gname])
                child.append(gchild)

            elem.append(child)

    return elem

market = Market()
print '<?xml version="1.0" encoding="utf-8"?>%s' % tostring(obj_to_xml(market))
๐Ÿ‘คmariodev

Leave a comment