Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:14:23+00:00 2026-06-09T07:14:23+00:00

I am trying to create an XML string from the values in a dictionary

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:14:25+00:00Added an answer on June 9, 2026 at 7:14 am

    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

    def _dict_to_xml(dictionary):
        returnlist = []
        for key in dictionary:
            if isinstance(dictionary[key],dict):
                returnlist.append(r'<node name="{name}">'.format(name=key))
                returnlist.append(_dict_to_xml(dictionary[key]))
                returnlist.append(r'</node>')
            else:
                returnlist.append(r'<order>{key}</order>'.format(key=key))
                returnlist.append(r'<string>{value}</string>'.format(value = dictionary[key]))
        return '\n'.join(returnlist)
    
    
    def dict_to_xml(dictionary):
        return '<?xml version="1.0"?>\n'+_dict_to_xml(dictionary)+'</xml>'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a .resx file from a dictionary of strings. Luckily the
I am trying to extract values from an XML file using linq to create
I'm trying to create an XML sitemap using CakePHP, from a table which has
Hi I am trying to read xml string and trying to replace from old
I am trying to select specific values from a xml document using XPath. The
I am trying to select specific values from a xml document using XPath. The
I am trying to create an XML file based on data fields from a
Im trying to write a List of 'Documents' from an XML string, but i
I am trying to create an XML file, but the XML file needs to
I'm trying to create an xml entry that looks like this using python and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.