X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=example%2Fdiameter%2Flauncher%2Fresources%2Frest_api%2Fhelpers%2FdiameterJsonHelper%2Fxml2json.py;h=bb25d8e8e5f6327c69d6e8f826336f5e5bfdc9fe;hb=HEAD;hp=00c632632fec97f42ba2ed050df97fd154ea10bd;hpb=c881c12ed7e116f1d43760a0d9873f860c10a357;p=anna.git diff --git a/example/diameter/launcher/resources/rest_api/helpers/diameterJsonHelper/xml2json.py b/example/diameter/launcher/resources/rest_api/helpers/diameterJsonHelper/xml2json.py index 00c6326..bb25d8e 100755 --- a/example/diameter/launcher/resources/rest_api/helpers/diameterJsonHelper/xml2json.py +++ b/example/diameter/launcher/resources/rest_api/helpers/diameterJsonHelper/xml2json.py @@ -1,4 +1,16 @@ #!/usr/bin/python3 +# +# Converts Anna-Diameter XML files to JSON format. +# Useful to build json content for ADML REST API: +# * Services: +# python3 xml2json.py services.xml servicesJson +# +# * Diameter messages: +# python3 xml2json.py CCR.xml diameterJson +# +# * Just xml to json: +# python3 xml2json.py file.xml + import xmltodict import json import sys @@ -6,7 +18,15 @@ import sys try: file = sys.argv[1] except: - print("Usage: " + sys.argv[0] + " ") + print("Usage: " + sys.argv[0] + " [parent name (i.e.: servicesJson, diameterJson), none by default]\n") + print(" Converts Anna-Diameter XML files to JSON format.") + print(" Useful to build json content for ADML REST API:\n") + print(" - Services:") + print(" python3 xml2json.py services.xml servicesJson\n") + print(" - Diameter messages:") + print(" python3 xml2json.py CCR.xml diameterJson\n") + print(" - Direct xml into json format:") + print(" python3 xml2json.py file.xml") sys.exit(1) try: @@ -16,10 +36,19 @@ except: print("ERROR reading '" + file + "'") sys.exit(1) +parent = None +try: + parent = sys.argv[2] +except: + pass + # although default attribute prefix is '@', anyway we will # force the value just in case xmltodict implementation # changes. The anna::core::functions::json2xml helper, # assumes this prefix in order to work properly. -my_dict=xmltodict.parse(xml, attr_prefix='@') -json_data=json.dumps(my_dict, indent=3, sort_keys=True) -print(json_data) +result=xmltodict.parse(xml, force_list={'avp'}, attr_prefix='@') + +if parent: + result = { parent: result } + +print (json.dumps(result, indent=3, sort_keys=True))