看代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-


class DicXl(object):

    def dict_to_xml(self, params):
        def demo(params=params):
            str = ""
            for k, v in params.items():
                str += "<%s>""%s""<%s>" % (k, v, k) if not isinstance(v, dict) else "<%s>""%s""<%s>" % (k, demo(v), k)
            message = "<MbfBody>" + str + "</MbfBody>"
            return message

        return demo

if __name__ == '__main__':
    params = {"name": "小明", "age": 18, "hobby": "football", "other": {"name": "火云邪神"}}
    print(DicXl().dict_to_xml(params)())
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐