简体   繁体   English

python-如何在没有root的情况下将xml转换为json?

[英]How to convert an xml to json without root in Python?

I need to convert XML to json without root in python. 我需要将XML转换为json,而无需在python中建立根。 Here is an example of XML 这是XML的示例

  <?xml version="1.0" encoding="UTF-8"?>
<root>
  <row>
    <Member_ID>926494</Member_ID>
    <First_Name>Corissa</First_Name>
    <Last_Name>Aguiler</Last_Name>
    <Gender>F</Gender>
    <Age>39</Age>
    <Height>5,3</Height>
    <Weight>130</Weight>
    <Hours_Sleep>8</Hours_Sleep>
    <Calories_Consumed>2501</Calories_Consumed>
    <Exercise_Calories_Burned>990</Exercise_Calories_Burned>
    <Date>9/11/2017</Date>
  </row>
</root>

I need to convert into JSON in the following format 我需要将以下格式转换为JSON

   {
    "Member_ID": 926494,
    "First_Name": "Corissa",
    "Last_Name": "Aguiler",
    "Gender": "F",
    "Age": 39,
    "Height": "5,3",
    "Weight": 130,
    "Hours_Sleep": 8,
    "Calories_Consumed": 2501,
    "Exercise_Calories_Burned": 990,
    "Date": "9/11/2017"
  },

I am trying to use the parker convention from xmljson library but all the examples I'm finding are using string as input. 我正在尝试使用xmljson库中的parker约定,但是我发现的所有示例都使用字符串作为输入。 I can't seem to figure out how to pass the actual .xml file instead of a string 我似乎无法弄清楚如何传递实际的.xml文件而不是字符串

For example: 例如:

from xmljson import parker, Parker
from xml.etree.ElementTree import fromstring
from json import dumps
dumps(parker.data(fromstring('<x><a>1</a><b>2</b></x>')))
'{"a": 1, "b": 2}'

You can use standart xml library to parse it to dict and then dump dict to json if it needed: 您可以使用standart xml库将其解析为dict,然后在需要时将dict转储到json:

xml_raw = """<?xml version="1.0" encoding="UTF-8"?>
<root>
  <row>
    <Member_ID>926494</Member_ID>
    <First_Name>Corissa</First_Name>
    <Last_Name>Aguiler</Last_Name>
    <Gender>F</Gender>
    <Age>39</Age>
    <Height>5,3</Height>
    <Weight>130</Weight>
    <Hours_Sleep>8</Hours_Sleep>
    <Calories_Consumed>2501</Calories_Consumed>
    <Exercise_Calories_Burned>990</Exercise_Calories_Burned>
    <Date>9/11/2017</Date>
  </row>
  <row>
    <Member_ID>926494</Member_ID>
    <First_Name>Corissa</First_Name>
    <Last_Name>Aguiler</Last_Name>
    <Gender>F</Gender>
    <Age>39</Age>
    <Height>5,3</Height>
    <Weight>130</Weight>
    <Hours_Sleep>8</Hours_Sleep>
    <Calories_Consumed>2501</Calories_Consumed>
    <Exercise_Calories_Burned>990</Exercise_Calories_Burned>
    <Date>9/11/2017</Date>
  </row>
</root>"""

import xml.etree.ElementTree as ET

root = ET.fromstring(xml_raw)

xml_dict_list = list()
for row in root.findall('.//row'):
    xml_dict = dict()
    for item in row.findall('./*'):
        xml_dict[item.tag] = item.text
    xml_dict_list.append(xml_dict)

print('dict ->', xml_dict_list)
import json

json_str = json.dumps(xml_dict_list)
print('str ->', json_str)

OUTPUT: OUTPUT:

dict -> [{'Member_ID': '926494', 'First_Name': 'Corissa', 'Last_Name': 'Aguiler', 'Gender': 'F', 'Age': '39', 'Height': '5,3', 'Weight': '130', 'Hours_Sleep': '8', 'Calories_Consumed': '2501', 'Exercise_Calories_Burned': '990', 'Date': '9/11/2017'}, {'Member_ID': '926494', 'First_Name': 'Corissa', 'Last_Name': 'Aguiler', 'Gender': 'F', 'Age': '39', 'Height': '5,3', 'Weight': '130', 'Hours_Sleep': '8', 'Calories_Consumed': '2501', 'Exercise_Calories_Burned': '990', 'Date': '9/11/2017'}]
str -> [{"Member_ID": "926494", "First_Name": "Corissa", "Last_Name": "Aguiler", "Gender": "F", "Age": "39", "Height": "5,3", "Weight": "130", "Hours_Sleep": "8", "Calories_Consumed": "2501", "Exercise_Calories_Burned": "990", "Date": "9/11/2017"}, {"Member_ID": "926494", "First_Name": "Corissa", "Last_Name": "Aguiler", "Gender": "F", "Age": "39", "Height": "5,3", "Weight": "130", "Hours_Sleep": "8", "Calories_Consumed": "2501", "Exercise_Calories_Burned": "990", "Date": "9/11/2017"}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM