4
4
import requests
5
5
import simplejson
6
6
import dict2xml
7
+ import json
7
8
from bs4 import BeautifulSoup
8
9
9
10
@@ -52,6 +53,18 @@ def fromurl(cls, url: str):
52
53
else :
53
54
raise Exception ("Bad URl, Can't get JSON response" )
54
55
56
+ @classmethod
57
+ def fromstring (cls , data : str ):
58
+ if type (data ) is not str :
59
+ raise ("Sorry but it doesn't seem to be valid string" )
60
+ try :
61
+ data = json .loads (data )
62
+ except Exception as e :
63
+ print ("Sorry, failed to load json, seems the JSON is not right" )
64
+ data = []
65
+ return cls (data )
66
+
67
+
55
68
# -------------------------------
56
69
##
57
70
# @Synopsis This method actually
@@ -71,6 +84,7 @@ def main(argv=None):
71
84
parser = argparse .ArgumentParser (description = 'Utility to convert json to valid xml.' )
72
85
parser .add_argument ('--url' , dest = 'url' , action = 'store' )
73
86
parser .add_argument ('--file' , dest = 'file' , action = 'store' )
87
+ parser .add_argument ('--data' , dest = 'data' , action = 'store' )
74
88
args = parser .parse_args ()
75
89
76
90
if args .url :
@@ -83,6 +97,10 @@ def main(argv=None):
83
97
data = Json2xml .fromjsonfile (file )
84
98
print (Json2xml .json2xml (data ))
85
99
100
+ if args .data :
101
+ str_data = args .data
102
+ data = Json2xml .fromstring (str_data )
103
+ print (Json2xml .json2xml (data ))
86
104
87
105
if __name__ == "__main__" :
88
106
main (sys .argv )
0 commit comments