I'm using xmltodict to parse an XML config. The XML has structures where an element can occur in 1 to n instances, where both are valid:
<items>
<item-ref>abc</item-ref>
</items>
and
<items>
<item-ref>abc</item-ref>
<item-ref>dca</item-ref>
<item-ref>abb</item-ref>
</items>
I'm parsing this with xmltodict as follows:
document['items']['item-ref']
and it gives back a single unicode or a list (depending the items found), so I always need to add an extra check to ensure if I need to handle a list or a string:
if isinstance(document['items']['item-ref'], list):
my_var = document['items']['item-ref']
else:
my_var = [document['items']['item-ref']] #create list manually
Is there a better/simpler/more elegant way to handle these?