在Python中将JSON转换为XML

时间:2023-01-15 07:39:54

I see a number of questions on SO asking about ways to convert XML to JSON, but I'm interested in going the other way. Is there a python library for converting JSON to XML?

我看到了许多关于如何将XML转换为JSON的问题,但我对另一种方式感兴趣。有python库可以将JSON转换为XML吗?


Edit: Nothing came back right away, so I went ahead and wrote a script that solves this problem.

编辑:没有任何东西马上回来,所以我继续写了一个解决这个问题的脚本。

Python already allows you to convert from JSON into a native dict (using json or, in versions < 2.6, simplejson), so I wrote a library that converts native dicts into an XML string.

Python已经允许您将JSON转换为本地命令(使用JSON或版本< 2.6,simplejson),因此我编写了一个将本地命令转换为XML字符串的库。

https://github.com/quandyfactory/dict2xml

https://github.com/quandyfactory/dict2xml

It supports int, float, boolean, string (and unicode), array and dict data types and arbitrary nesting (yay recursion).

它支持int、float、boolean、string(和unicode)、array和dict数据类型以及任意嵌套(yay递归)。

I'll post this as an answer once 8 hours have passed.

8个小时过去后,我将把这个作为一个答案。

3 个解决方案

#1


27  

Nothing came back right away, so I went ahead and wrote a script that solves this problem.

没有任何东西马上回来,所以我继续写了一个解决这个问题的脚本。

Python already allows you to convert from JSON into a native dict (using json or, in versions < 2.6, simplejson), so I wrote a library that converts native dicts into an XML string.

Python已经允许您将JSON转换为本地命令(使用JSON或版本< 2.6,simplejson),因此我编写了一个将本地命令转换为XML字符串的库。

https://github.com/quandyfactory/dict2xml

https://github.com/quandyfactory/dict2xml

It supports int, float, boolean, string (and unicode), array and dict data types and arbitrary nesting (yay recursion).

它支持int、float、boolean、string(和unicode)、array和dict数据类型以及任意嵌套(yay递归)。

#2


12  

Load it into a dict using json.loads then use anything from this question...

使用json加载它。然后用这个问题的任何东西……

Serialize Python dictionary to XML

将Python字典序列化为XML

#3


9  

If you don't have such a package, you can try:

如果你没有这样的包装,你可以试试:

def json2xml(json_obj, line_padding=""):
    result_list = list()

    json_obj_type = type(json_obj)

    if json_obj_type is list:
        for sub_elem in json_obj:
            result_list.append(json2xml(sub_elem, line_padding))

        return "\n".join(result_list)

    if json_obj_type is dict:
        for tag_name in json_obj:
            sub_obj = json_obj[tag_name]
            result_list.append("%s<%s>" % (line_padding, tag_name))
            result_list.append(json2xml(sub_obj, "\t" + line_padding))
            result_list.append("%s</%s>" % (line_padding, tag_name))

        return "\n".join(result_list)

    return "%s%s" % (line_padding, json_obj)

For example:

例如:

s='{"main" : {"aaa" : "10", "bbb" : [1,2,3]}}'
j = json.loads(s)
print(json2xml(j))

Result:

结果:

<main>
        <aaa>
                10
        </aaa>
        <bbb>
                1
                2
                3
        </bbb>
</main>

#1


27  

Nothing came back right away, so I went ahead and wrote a script that solves this problem.

没有任何东西马上回来,所以我继续写了一个解决这个问题的脚本。

Python already allows you to convert from JSON into a native dict (using json or, in versions < 2.6, simplejson), so I wrote a library that converts native dicts into an XML string.

Python已经允许您将JSON转换为本地命令(使用JSON或版本< 2.6,simplejson),因此我编写了一个将本地命令转换为XML字符串的库。

https://github.com/quandyfactory/dict2xml

https://github.com/quandyfactory/dict2xml

It supports int, float, boolean, string (and unicode), array and dict data types and arbitrary nesting (yay recursion).

它支持int、float、boolean、string(和unicode)、array和dict数据类型以及任意嵌套(yay递归)。

#2


12  

Load it into a dict using json.loads then use anything from this question...

使用json加载它。然后用这个问题的任何东西……

Serialize Python dictionary to XML

将Python字典序列化为XML

#3


9  

If you don't have such a package, you can try:

如果你没有这样的包装,你可以试试:

def json2xml(json_obj, line_padding=""):
    result_list = list()

    json_obj_type = type(json_obj)

    if json_obj_type is list:
        for sub_elem in json_obj:
            result_list.append(json2xml(sub_elem, line_padding))

        return "\n".join(result_list)

    if json_obj_type is dict:
        for tag_name in json_obj:
            sub_obj = json_obj[tag_name]
            result_list.append("%s<%s>" % (line_padding, tag_name))
            result_list.append(json2xml(sub_obj, "\t" + line_padding))
            result_list.append("%s</%s>" % (line_padding, tag_name))

        return "\n".join(result_list)

    return "%s%s" % (line_padding, json_obj)

For example:

例如:

s='{"main" : {"aaa" : "10", "bbb" : [1,2,3]}}'
j = json.loads(s)
print(json2xml(j))

Result:

结果:

<main>
        <aaa>
                10
        </aaa>
        <bbb>
                1
                2
                3
        </bbb>
</main>