I am trying to create an XML string from the values in a dictionary with the following structure. The number of keys (dictionary depth) from the root to the strings is indeterminate, ranging from 1 to ?.
'modes': { 'P': { 'S': { u'01': u'Some Text A.',
u'02': u'Some Text B.',
u'03': u'Some Text C.',
u'04': u'Some Text D.',
u'05': u'Some Text E.',
u'06': u'Some Text F.'},
'U': { u'01': u'Some Text G.',
u'02': u'Some Text H.'}},
'R': { 'S': { u'01': u'Some Text I.',
u'02': u'Some Text J.',
u'03': u'Some Text K.',
u'04': u'Some Text M.',
u'05': u'LSome Text N.'},
'U': { u'01': u'Some Text O.',
u'02': u'Some Text P.',
u'03': u'Some Text Q.'}}}
An example of the output I’m after would be:
<modes>
<property P>
<property S>
<text>
<order>'01'</order>
<string>'Some Text A.'</string>
</text>
<text>
<order>'02'</order>
<string>'Some Text B.'</string>
</text>
...
</property S>
<property U>
<text>
<order>'01'</order>
<string>'Some Text G.'</string>
</text>
<text>
<order>'02'</order>
<string>'Some Text H.'</string>
</text>
</property U>
</property P>
<property R>
<property S>
<text>
<order>'01'</order>
<string>'Some Text I.'</string>
</text>
<text>
<order>'02'</order>
<string>'Some Text J.'</string>
</text>
...
</property S>
<property U>
<text>
<order>'01'</order>
<string>'Some Text O.'</string>
</text>
<text>
<order>'02'</order>
<string>'Some Text P.'</string>
</text>
...
</property U>
</property R>
</modes>
I’m more interested in how to iterate the structure such that I can put the children in the right parents, rather than the exact output as XML. Any advise about maybe altering the structure of the data would also be appreciated, as I feel like I have painted myself into a corner!
Thanks Julian
They way I found was to use a recursive function that would print key,value if dictionary[key] was not a dict, and otherwise print the recursive call