3 # Converts Anna-Diameter XML files to JSON format.
4 # Useful to build json content for ADML REST API:
6 # python3 xml2json.py services.xml servicesJson
9 # python3 xml2json.py CCR.xml diameterJson
12 # python3 xml2json.py file.xml
21 print("Usage: " + sys.argv[0] + " <diameter xml file> [parent name (i.e.: servicesJson, diameterJson), none by default]\n")
22 print(" Converts Anna-Diameter XML files to JSON format.")
23 print(" Useful to build json content for ADML REST API:\n")
25 print(" python3 xml2json.py services.xml servicesJson\n")
26 print(" - Diameter messages:")
27 print(" python3 xml2json.py CCR.xml diameterJson\n")
28 print(" - Direct xml into json format:")
29 print(" python3 xml2json.py file.xml")
33 with open(file, 'r') as myfile:
36 print("ERROR reading '" + file + "'")
45 # although default attribute prefix is '@', anyway we will
46 # force the value just in case xmltodict implementation
47 # changes. The anna::core::functions::json2xml helper,
48 # assumes this prefix in order to work properly.
49 result=xmltodict.parse(xml, force_list={'avp'}, attr_prefix='@')
52 result = { parent: result }
54 print (json.dumps(result, indent=3, sort_keys=True))