-
Notifications
You must be signed in to change notification settings - Fork 6
/
xml2json.py
executable file
·44 lines (33 loc) · 1.14 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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import sys
import argparse
parser = argparse.ArgumentParser(description='Convert an XML file to JSON.')
parser.add_argument('--unsafe', action='store_true',
help='do not use defusedxml: only for known-safe XML!')
parser.add_argument('infile', nargs='?', type=argparse.FileType('rt'),
default=sys.stdin)
def xml_element_to_dict(elem):
"Convert XML Element to a simple dict"
inner = dict(elem.attrib)
children = list(map(xml_element_to_dict, list(elem)))
text = elem.text and elem.text.strip()
if text:
inner['@text'] = text
if children:
inner['@children'] = children
return {elem.tag: inner}
def main(args):
"Dump JSON-from-parsed-XML to stdout"
if args.unsafe:
import xml.etree.cElementTree as ElementTree
else:
import defusedxml.cElementTree as ElementTree
xml_parser = ElementTree.parse(args.infile)
root = xml_parser.getroot()
json.dump(xml_element_to_dict(root), sys.stdout, indent=2)
print()
if __name__ == '__main__':
args = parser.parse_args()
main(args)