python递归方法 dict 转 xml

dd = {"a":"a","b":{"cc":"cc","dd":{"eee":"eee"}}}

def dict_to_xml(data):
    def to_xml(data):
        xx = []
        for k,v in data.iteritems():
            if isinstance(v,dict):
                aa = to_xml(v)
                s = "<{key}>{value}".format(key=k,value=aa)
            else:
                s = "<{key}>{value}".format(key=k,value=v)
            xx.append(s)
        return ''.join(xx)

    return ''+to_xml(data)+''

print dict_to_xml(dd)

你可能感兴趣的:(Python)