在python中将数组dict转换为xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36021526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:18:12  来源:igfitidea点击:

Converting an array dict to xml in python?

pythonarraysxml

提问by user875139

I have this array that I need to convert to xml.

我有这个数组需要转换为 xml。

array = [
    {
        'time': {"hour":"1", "minute":"30","seconds": "40"}
    },
    {
        'place': {"street":"40 something", "zip": "00000"}
    }
]

The xml should have a title that I can put in as a variable for example,

xml 应该有一个标题,我可以把它作为一个变量,例如,

xml_title = "test"

The result I want based on the array above and the xml title is this:

根据上面的数组和xml标题,我想要的结果是这样的:

<test>
    <time hour="1" minute="30" second="40"></time>
    <place>
        <street>40 something</street>
        <zip>00000</zip>
    </place>
</test>

I liked the answer given in a similar stack overflow question (https://stackoverflow.com/a/18991263/875139) but I am confused how I can use that answer to get this desired result.

我喜欢在类似的堆栈溢出问题(https://stackoverflow.com/a/18991263/875139)中给出的答案,但我很困惑如何使用该答案来获得所需的结果。

Help please.

请帮忙。

回答by Jared Goguen

As noted in the comments, your original question mixes attributes and elements. If you want everything as elements, you might be able to use dicttoxml. For example:

如评论中所述,您的原始问题混合了属性和元素。如果您希望所有内容都作为元素,则可以使用dicttoxml. 例如:

from dicttoxml import dicttoxml

array = [
    {
        'time': {"hour":"1", "minute":"30","seconds": "40"}
    },
    {
        'place': {"street":"40 something", "zip": "00000"}
    }
]

xml = dicttoxml(array, custom_root='test', attr_type=False)

Produces the following XML:

生成以下 XML:

<?xml version="1.0" encoding="UTF-8" ?>
<test>
    <item>
        <time>
            <seconds>40</seconds>
            <minute>30</minute>
            <hour>1</hour>
        </time>
    </item>
    <item>
        <place>
            <street>40 something</street>
            <zip>00000</zip>
        </place>
    </item>
</test>

If you can convert your dictionary to:

如果您可以将字典转换为:

dictionary = {
    'time': {"hour":"1", "minute":"30","seconds": "40"},
    'place': {"street":"40 something", "zip": "00000"}
}

Then your XML will look as desired.

然后您的 XML 将如您所愿。

<?xml version="1.0" encoding="UTF-8" ?>
<test>
    <place>
        <street>40 something</street>
        <zip>00000</zip>
    </place>
    <time>
        <seconds>40</seconds>
        <minute>30</minute>
        <hour>1</hour>
    </time>
</test>

Note that, in general, the order of dictionary keys are not guaranteed, so if you want to preserve the order of keys in a dict, you may want to check out collections.OrderedDict.

请注意,一般情况下,字典键的顺序是无法保证的,因此如果您想保留 a 中键的顺序dict,您可能需要查看collections.OrderedDict.

回答by OneCricketeer

I ended up taking the solution from here, then adding a for-loop over the elements in your array. The output uses attributes instead of elements like you had asked, though.

我最终从这里采用了解决方案,然后在数组中的元素上添加了一个 for 循环。但是,输出使用属性而不是您要求的元素。

Full code outside of that function is this. I ended up using regex to strip out the intermediate <test></test>tags, then placed the on the outside at the end.

该函数之外的完整代码是这样的。我最终使用正则表达式去除中间<test></test>标签,然后将 放在最后。

import re 

array = [
    {
        'time': {"hour":"1", "minute":"30","seconds": "40"}
    },
    {
        'place': {"street":"40 something", "zip": "00000"}
    }
]

xml_title = "test"
xml_tag_pattern = re.compile(r'</?{}>'.format(xml_title))
inner_xml = re.sub(xml_tag_pattern, '', ''.join(dict2xml(e, root_node=tag_name) for e in array))

print('<{0}>{1}</{0}>'.format(xml_title, inner_xml))

The output is this (new lines added for clarity)

输出是这样的(为清楚起见添加了新行)

<test>
    <time hour="1" seconds="40" minute="30"/>
    <place street="40 something" zip="00000"/>
</test>