-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml2json.py
72 lines (55 loc) · 2.3 KB
/
xml2json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import json
import xml.etree.ElementTree as ET
from glob import glob
def xml_to_dict(element):
"""
Recursively convert an XML element and its children to a dictionary.
"""
result = {}
if element.attrib:
result["@attributes"] = element.attrib
if element.text and element.text.strip():
result["#text"] = element.text.strip()
children = list(element)
if children:
child_dict = {}
for child in children:
child_result = xml_to_dict(child)
if child.tag in child_dict:
if not isinstance(child_dict[child.tag], list):
child_dict[child.tag] = [child_dict[child.tag]]
child_dict[child.tag].append(child_result[child.tag])
else:
child_dict[child.tag] = child_result[child.tag]
result.update(child_dict)
else:
# Set the leaf element's tag to its text content if present
if element.text and element.text.strip():
result = element.text.strip()
else:
result = None
return {element.tag: result}
def convert_xml_to_json(input_folder, output_folder):
"""
Convert all XML files in the input folder to JSON files in the output folder.
"""
if not os.path.exists(output_folder):
os.makedirs(output_folder)
xml_files = glob(os.path.join(input_folder, "*.xml"))
for xml_file in xml_files:
try:
tree = ET.parse(xml_file)
root = tree.getroot()
data_dict = xml_to_dict(root)
json_filename = os.path.splitext(os.path.basename(xml_file))[0] + ".json"
json_filepath = os.path.join(output_folder, json_filename)
with open(json_filepath, "w", encoding="utf-8") as json_file:
json.dump(data_dict, json_file, indent=4, ensure_ascii=False)
print(f"Converted: {xml_file} -> {json_filepath}")
except Exception as e:
print(f"Error processing {xml_file}: {e}")
if __name__ == "__main__":
input_folder = input("Enter the path to the folder containing XML files: ")
output_folder = input("Enter the path to the output folder for JSON files: ")
convert_xml_to_json(input_folder, output_folder)