15
15
from flattentool .sheet import Sheet
16
16
from warnings import warn
17
17
import codecs
18
+ import xmltodict
18
19
19
20
BASIC_TYPES = [six .text_type , bool , int , Decimal , type (None )]
20
21
@@ -45,12 +46,14 @@ class JSONParser(object):
45
46
# Named for consistency with schema.SchemaParser, but not sure it's the most appropriate name.
46
47
# Similarily with methods like parse_json_dict
47
48
48
- def __init__ (self , json_filename = None , root_json_dict = None , schema_parser = None , root_list_path = None , root_id = 'ocid' , use_titles = False ):
49
+ def __init__ (self , json_filename = None , root_json_dict = None , schema_parser = None , root_list_path = None ,
50
+ root_id = 'ocid' , use_titles = False , xml = False , id_name = 'id' ):
49
51
self .sub_sheets = {}
50
52
self .main_sheet = Sheet ()
51
53
self .root_list_path = root_list_path
52
54
self .root_id = root_id
53
55
self .use_titles = use_titles
56
+ self .id_name = id_name
54
57
if schema_parser :
55
58
self .main_sheet = schema_parser .main_sheet
56
59
self .sub_sheets = schema_parser .sub_sheets
@@ -60,6 +63,11 @@ def __init__(self, json_filename=None, root_json_dict=None, schema_parser=None,
60
63
else :
61
64
self .rollup = False
62
65
66
+ if xml :
67
+ with codecs .open (json_filename , 'rb' ) as xml_file :
68
+ root_json_dict = xmltodict .parse (xml_file , cdata_key = '' )['iati-activities' ]
69
+ json_filename = None
70
+
63
71
if json_filename is None and root_json_dict is None :
64
72
raise ValueError ('Etiher json_filename or root_json_dict must be supplied' )
65
73
@@ -114,8 +122,8 @@ def parse_json_dict(self, json_dict, sheet, json_key=None, parent_name='', flatt
114
122
if self .root_id and self .root_id in json_dict :
115
123
parent_id_fields [sheet_key (sheet , self .root_id )] = json_dict [self .root_id ]
116
124
117
- if 'id' in json_dict :
118
- parent_id_fields [sheet_key (sheet , parent_name + 'id' )] = json_dict ['id' ]
125
+ if self . id_name in json_dict :
126
+ parent_id_fields [sheet_key (sheet , parent_name + self . id_name )] = json_dict [self . id_name ]
119
127
120
128
121
129
for key , value in json_dict .items ():
0 commit comments