All pastes #1510043 Raw Edit

Stuff

public text v1 · immutable
#1510043 ·published 2009-07-28 15:08 UTC
rendered paste body
def json2xml(dictionary, buffer=None):
    if buffer is None:
        buffer = StringIO()
    for key, value in dictionary.items():
        if isinstance(value, basestring):
            print >> buffer, '<%s>%s</%s>' % (key, value, key)
            continue
        if value is None:
            print >> buffer, '<%s/>' % key
            continue
        if isinstance(value, dict):
            print >> buffer, '<%s>' % key
            json2xml(value, buffer)
            print >> buffer, '</%s>' % key
        else:
            # iterables
            for item in value:
                json2xml({key: item}, buffer)
    return buffer.getvalue()

if __name__ == '__main__':
    json = { 'foo': { 'bar': 'fleem', 'item': ('cats', 'dogs', 'pigs')}}
    print json2xml(json)