-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxml2json.py
More file actions
66 lines (49 loc) · 1.61 KB
/
xml2json.py
File metadata and controls
66 lines (49 loc) · 1.61 KB
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
import sys
import xml.etree.ElementTree as ET
import json
import pathlib
def xml_to_dict(element):
result = {}
if element.attrib:
result['@attributes'] = element.attrib
children = list(element)
if children:
child_dict = {}
for child in children:
child_data = 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_data)
else:
child_dict[child.tag] = child_data
result.update(child_dict)
if element.text and element.text.strip():
if result:
result['@text'] = element.text.strip()
else:
return element.text.strip()
return result if result else None
def main():
if len(sys.argv) != 3:
print("Usage: xml2json.py <input-xml> <output-json>", file=sys.stderr)
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
try:
with open(input_file, 'r') as file:
xml_data = file.read()
root = ET.fromstring(xml_data)
result = {
root.tag: xml_to_dict(root)
}
output_path = pathlib.Path(output_file)
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, 'w') as file:
json.dump(result, file, indent=2)
file.write('\n')
except Exception as error:
print(f"Error: {error}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()