在Python中将JSON转换为XML

iecba09b  于 11个月前  发布在  Python
关注(0)|答案(6)|浏览(120)

我在SO上看到了很多关于如何将XML转换为JSON的问题,但我对另一种方法感兴趣。

**编辑:**没有立即返回,所以我继续写了一个脚本来解决这个问题。

Python已经允许您将JSON转换为原生字典(使用json,或者在版本< 2.6时,使用simplejson),因此我编写了一个库,将原生字典转换为XML字符串。
https://github.com/quandyfactory/dict2xml
它支持int,float,boolean,string(和unicode),array和dict数据类型以及任意嵌套(yay递归)。
我会把这个作为一个答案,一旦8小时过去了。

8iwquhpp

8iwquhpp1#

没有立即返回,所以我继续写了一个脚本来解决这个问题。
Python已经允许您将JSON转换为原生字典(使用json,或者在版本< 2.6时,使用simplejson),因此我编写了一个库,将原生字典转换为XML字符串。
https://github.com/quandyfactory/dict2xml
它支持int,float,boolean,string(和unicode),array和dict数据类型以及任意嵌套(yay递归)。

c86crjj0

c86crjj02#

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

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)

字符串
举例来说:

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


测试结果:

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

s8vozzvw

s8vozzvw3#

使用json将其加载到dict中。加载然后使用此问题中的任何内容.
Serialize Python dictionary to XML

fnx2tebb

fnx2tebb4#

使用dicttoxmlJSON直接转换为XML

安装

pip install dicttoxml

easy_install dicttoxml

In [2]: from json import loads

In [3]: from dicttoxml import dicttoxml

In [4]: json_obj = '{"main" : {"aaa" : "10", "bbb" : [1,2,3]}}'

In [5]: xml = dicttoxml(loads(json_obj))

In [6]: print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><main type="dict"><aaa type="str">10</aaa><bbb type="list"><item type="int">1</item><item type="int">2</item><item type="int">3</item></bbb></main></root>

In [7]: xml = dicttoxml(loads(json_obj), attr_type=False)

In [8]: print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><main><aaa>10</aaa><bbb><item>1</item><item>2</item><item>3</item></bbb></main></root>

字符串
有关dicttoxml的更多信息

anhgbhbe

anhgbhbe5#

我发现xmltodict很有用。看起来它是在这里的一些帖子之后发布的。https://pypi.org/project/xmltodict/

import xmltodict
import json
sample_json = {"note": {"to": "Tove", "from": "Jani", "heading": "Reminder", "body": "Don't forget me this weekend!"}}
#############
#json to xml
#############
json_to_xml = xmltodict.unparse(sample_json)
print(json_to_xml)
#############
#xml to json
#############
x_to_j_dict = xmltodict.parse(json_to_xml)
x_to_j_string = json.dumps(x_to_j_dict)
back_to_json = json.loads(x_to_j_string)
print(back_to_json)

字符串

bkhjykvo

bkhjykvo6#

from json import loads
from dicttoxml import dicttoxml

s='{"main" : {"aaa" : "10", "bbb" : [1,2,3]}}'
xml = dicttoxml(loads(s))

字符串
或者,如果您的数据存储在pandas data.frame中,我的数据通常是:

df['xml'] = df['json'].apply(lambda s: dicttoxml(json.loads(s))

相关问题