@@ -134,7 +134,7 @@ def default_item_func(parent):
134
134
return "item"
135
135
136
136
137
- def convert (obj , ids , attr_type , item_func , cdata , parent = "root" ):
137
+ def convert (obj , ids , attr_type , item_func , cdata , item_wrap , parent = "root" ):
138
138
"""Routes the elements of an object to the right function to convert them
139
139
based on their data type"""
140
140
@@ -157,15 +157,15 @@ def convert(obj, ids, attr_type, item_func, cdata, parent="root"):
157
157
return convert_none (item_name , "" , attr_type , cdata )
158
158
159
159
if isinstance (obj , dict ):
160
- return convert_dict (obj , ids , parent , attr_type , item_func , cdata )
160
+ return convert_dict (obj , ids , parent , attr_type , item_func , cdata , item_wrap )
161
161
162
162
if isinstance (obj , collections .Iterable ):
163
- return convert_list (obj , ids , parent , attr_type , item_func , cdata )
163
+ return convert_list (obj , ids , parent , attr_type , item_func , cdata , item_wrap )
164
164
165
165
raise TypeError ("Unsupported data type: %s (%s)" % (obj , type (obj ).__name__ ))
166
166
167
167
168
- def convert_dict (obj , ids , parent , attr_type , item_func , cdata ):
168
+ def convert_dict (obj , ids , parent , attr_type , item_func , cdata , item_wrap ):
169
169
"""Converts a dict into an XML string."""
170
170
LOG .info (
171
171
'Inside convert_dict(): obj type is: "%s", obj="%s"'
@@ -201,7 +201,7 @@ def convert_dict(obj, ids, parent, attr_type, item_func, cdata):
201
201
% (
202
202
key ,
203
203
make_attrstring (attr ),
204
- convert_dict (val , ids , key , attr_type , item_func , cdata ),
204
+ convert_dict (val , ids , key , attr_type , item_func , cdata , item_wrap ),
205
205
key ,
206
206
)
207
207
)
@@ -214,7 +214,7 @@ def convert_dict(obj, ids, parent, attr_type, item_func, cdata):
214
214
% (
215
215
key ,
216
216
make_attrstring (attr ),
217
- convert_list (val , ids , key , attr_type , item_func , cdata ),
217
+ convert_list (val , ids , key , attr_type , item_func , cdata , item_wrap ),
218
218
key ,
219
219
)
220
220
)
@@ -230,7 +230,7 @@ def convert_dict(obj, ids, parent, attr_type, item_func, cdata):
230
230
return "" .join (output )
231
231
232
232
233
- def convert_list (items , ids , parent , attr_type , item_func , cdata ):
233
+ def convert_list (items , ids , parent , attr_type , item_func , cdata , item_wrap ):
234
234
"""Converts a list into an XML string."""
235
235
LOG .info ("Inside convert_list()" )
236
236
output = []
@@ -258,23 +258,39 @@ def convert_list(items, ids, parent, attr_type, item_func, cdata):
258
258
259
259
elif isinstance (item , dict ):
260
260
if not attr_type :
261
- addline (
262
- "<%s>%s</%s>"
263
- % (
264
- item_name ,
265
- convert_dict (item , ids , parent , attr_type , item_func , cdata ),
266
- item_name ,
261
+ if (item_wrap ):
262
+ addline (
263
+ "<%s>%s</%s>"
264
+ % (
265
+ item_name ,
266
+ convert_dict (item , ids , parent , attr_type , item_func , cdata , item_wrap ),
267
+ item_name ,
268
+ )
269
+ )
270
+ else :
271
+ addline (
272
+ "%s"
273
+ % (
274
+ convert_dict (item , ids , parent , attr_type , item_func , cdata , item_wrap ),
275
+ )
267
276
)
268
- )
269
277
else :
270
- addline (
271
- '<%s type="dict">%s</%s>'
272
- % (
273
- item_name ,
274
- convert_dict (item , ids , parent , attr_type , item_func , cdata ),
275
- item_name ,
278
+ if (item_wrap ):
279
+ addline (
280
+ '<%s type="dict">%s</%s>'
281
+ % (
282
+ item_name ,
283
+ convert_dict (item , ids , parent , attr_type , item_func , cdata , item_wrap ),
284
+ item_name ,
285
+ )
286
+ )
287
+ else :
288
+ addline (
289
+ '%s'
290
+ % (
291
+ convert_dict (item , ids , parent , attr_type , item_func , cdata , item_wrap ),
292
+ )
276
293
)
277
- )
278
294
279
295
elif isinstance (item , collections .Iterable ):
280
296
if not attr_type :
@@ -283,7 +299,7 @@ def convert_list(items, ids, parent, attr_type, item_func, cdata):
283
299
% (
284
300
item_name ,
285
301
make_attrstring (attr ),
286
- convert_list (item , ids , item_name , attr_type , item_func , cdata ),
302
+ convert_list (item , ids , item_name , attr_type , item_func , cdata , item_wrap ),
287
303
item_name ,
288
304
)
289
305
)
@@ -293,7 +309,7 @@ def convert_list(items, ids, parent, attr_type, item_func, cdata):
293
309
% (
294
310
item_name ,
295
311
make_attrstring (attr ),
296
- convert_list (item , ids , item_name , attr_type , item_func , cdata ),
312
+ convert_list (item , ids , item_name , attr_type , item_func , cdata , item_wrap ),
297
313
item_name ,
298
314
)
299
315
)
@@ -361,6 +377,7 @@ def dicttoxml(
361
377
custom_root = "root" ,
362
378
ids = False ,
363
379
attr_type = True ,
380
+ item_wrap = True ,
364
381
item_func = default_item_func ,
365
382
cdata = False ,
366
383
):
@@ -377,6 +394,8 @@ def dicttoxml(
377
394
- item_func specifies what function should generate the element name for
378
395
items in a list.
379
396
Default is 'item'
397
+ - item_wrap specifies whether to nest items in array in <item/>
398
+ Default is True
380
399
- cdata specifies whether string values should be wrapped in CDATA sections.
381
400
Default is False
382
401
"""
@@ -387,6 +406,6 @@ def dicttoxml(
387
406
output = []
388
407
output .append ('<?xml version="1.0" encoding="UTF-8" ?>' )
389
408
output .append (
390
- f"<{ custom_root } >{ convert (obj , ids , attr_type , item_func , cdata , parent = custom_root )} </{ custom_root } >"
409
+ f"<{ custom_root } >{ convert (obj , ids , attr_type , item_func , cdata , item_wrap , parent = custom_root )} </{ custom_root } >"
391
410
)
392
411
return "" .join (output ).encode ("utf-8" )
0 commit comments