【问题标题】:Python read a xml 1 -M and create jsonPython 读取 xml 1 -M 并创建 json
【发布时间】:2015-07-11 17:38:12
【问题描述】:

我已经开发了一个 python 脚本来读取 xml 文件的标签,然后将结果转换为 json。 现在的问题是每个元素的 xml 都有一些标签(关系 1 - M)

<idpoint>1021</idpoint>
<tipopoint>1</tipopoint>
<latitude>45.188377380</latitude>
<longitude>8.612257004</longitude>
<previsione time="2015-07-11T12:00:00">
<id_tempo>1</id_tempo>
<desc_tempo>sereno</desc_tempo>
<symbol_day>1</symbol_day>
<temp>33</temp>
</previsione>
<previsione time="2015-07-11T18:00:00">
<id_tempo>1</id_tempo>
<desc_tempo>sereno</desc_tempo>
<symbol_day>1</symbol_day>
<temp>29</temp>
</previsione>

我的代码 python 读取了第一个标签,当我到达标签 previsione 时,在同一点重复 2 次,我取第一个标签 previsioni 的第一个值,但不取第二个。 我可以重新创建相同的记录,但这次取第二个标签 previsioni 的值。

这是我的 python 代码的 sn-p

json_array = [];

for path in files:
    with open(path, 'r') as fr:
        print "Parsing xmldoc %s" % path

        xmldoc = minidom.parse(fr)

        if tipo == "allerte":
            items = xmldoc.getElementsByTagName("point")
        else:
            items = xmldoc.getElementsByTagName("localita")

        for item in items:
            obj = dict()

            if tipo == "allerte":
                obj['id'] = item.getElementsByTagName("idpoint")[0].firstChild.nodeValue



            else:
                obj['id'] = item.getElementsByTagName("idpoint")[0].firstChild.nodeValue

            obj['latitude'] = float(item.getElementsByTagName("latitude")[0].firstChild.nodeValue)
            obj['longitude'] = float(item.getElementsByTagName("longitude")[0].firstChild.nodeValue)
            #TODO: IL symbol code va recuperato dalla prima previsione
            sobj['symbolcode'] = int(item.getElementsByTagName("id_tempo")[0].firstChild.nodeValue)
            json_array.append(obj)

return json.dumps(json_array)

有什么帮助可以将此代码集成到 json 文件 2 元素的 2 标记关系中吗?

谢谢

【问题讨论】:

    标签: python json xml


    【解决方案1】:

    有一种从 xml 获取 json 的快速方法,使用 xmltodict。这个模块从你的 xml 中创建了很好的 dict,你可以像处理纯 json 一样轻松地操作你的数据。

    假设您的 xml 示例保存为 t2.xml 文件,并被 &lt;xml&gt;...&lt;/xml&gt; 标签包围。

    然后这个脚本

    #!/usr/bin/env python
    # coding: utf-8
    import sys
    import xmltodict
    import json
    
    with open('t2.xml', 'r') as data:
        print "Parsing xmldoc test.xml"
    
        dict = xmltodict.parse(data)
    
        print(json.dumps(dict, indent=4, sort_keys=True))
    

    会产生如下的json:

    {
        "xml": {
            "idpoint": "1021", 
            "latitude": "45.188377380", 
            "longitude": "8.612257004", 
            "previsione": [
                {
                    "@time": "2015-07-11T12:00:00", 
                    "desc_tempo": "sereno", 
                    "id_tempo": "1", 
                    "symbol_day": "1", 
                    "temp": "33"
                }, 
                {
                    "@time": "2015-07-11T18:00:00", 
                    "desc_tempo": "sereno", 
                    "id_tempo": "1", 
                    "symbol_day": "1", 
                    "temp": "29"
                }
            ], 
            "tipopoint": "1"
        }
    }
    

    特别是,您可以在数组中正确获取两个 previsione 元素,并且可以根据需要使用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-08
      • 2020-12-05
      • 1970-01-01
      相关资源
      最近更新 更多