61 lines
1.4 KiB
Python
Executable File
61 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import argparse
|
|
import json
|
|
import xmltodict
|
|
import sys
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument(
|
|
'infile',
|
|
type=argparse.FileType('r'),
|
|
nargs='?',
|
|
default=sys.stdin,
|
|
)
|
|
parser.add_argument(
|
|
'outfile',
|
|
type=argparse.FileType('w'),
|
|
nargs='?',
|
|
default=sys.stdout,
|
|
)
|
|
parser.add_argument(
|
|
'-s',
|
|
'--short-empty',
|
|
help='Shorten empty elements to <a/> instead of <a></a>',
|
|
action='store_true',
|
|
default=False,
|
|
)
|
|
parser.add_argument(
|
|
'-p',
|
|
'--pretty',
|
|
help='Indent XML with tabs and newlines.',
|
|
action='store_true',
|
|
default=False,
|
|
)
|
|
parser.add_argument(
|
|
'-ns',
|
|
'--namespace',
|
|
help='Add a namespace abbreviation.\n'
|
|
'Use the empty string as a name to set a default namespace.\n'
|
|
'xmltodict will expect the URL in your JSON objects:\n'
|
|
'{"http://my/ns:tag": "something"}',
|
|
action='append',
|
|
nargs=2,
|
|
default=[],
|
|
metavar=('URL', 'NAME'),
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
args.outfile.write(xmltodict.unparse(
|
|
json.load(args.infile),
|
|
pretty=args.pretty,
|
|
namespaces=dict(args.namespace),
|
|
short_empty_elements=args.short_empty,
|
|
))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|