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 130363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:56:11+00:00 2026-05-11T05:56:11+00:00

It’s a small thing, really: I have this function that converts dict objects to

  • 0

It’s a small thing, really: I have this function that converts dict objects to xml.

Here’s the function:

def dictToXml(d):     from xml.sax.saxutils import escape      def unicodify(o):         if o is None:             return u'';         return unicode(o)      lines = []     def addDict(node, offset):         for name, value in node.iteritems():             if isinstance(value, dict):                 lines.append(offset + u'<%s>' % name)                 addDict(value, offset + u' ' * 4)                 lines.append(offset + u'</%s>' % name)             elif isinstance(value, list):                 for item in value:                     if isinstance(item, dict):                         lines.append(offset + u'<%s>' % name)                         addDict(item, offset + u' ' * 4)                         lines.append(offset + u'</%s>' % name)                     else:                         lines.append(offset + u'<%s>%s</%s>' % (name, escape(unicodify(item)), name))             else:                 lines.append(offset + u'<%s>%s</%s>' % (name, escape(unicodify(value)), name))      addDict(d, u'')     lines.append(u'')     return u'\n'.join(lines) 

For example, it converts this dictionary

{ 'site': { 'name': 'stackoverflow', 'blogger': [ 'Jeff', 'Joel' ] } } 

to:

<site>     <name>stackoverflow</name>     <blogger>jeff</blogger>     <blogger>joel</blogger> </site> 

It works, but the addDict function looks a little too repetitive. I’m sure there’s a way to refactor it into 3 co-recursive functions named addDict, addList and addElse, but my brain is stuck. Any help?

Also, any way to get rid of the offset + thing in every line would be nice.

NOTE: I chose these semantics because I’m trying to match the behavior of the json-to-xml converter in org.json, which I use in a different part of my project. If you got to this page just looking for a dictionary to xml converter, there are some really good options in some of the answers. (Especially pyfo).

  • 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. 2026-05-11T05:56:11+00:00Added an answer on May 11, 2026 at 5:56 am

    I noticed you have commonality in adding items. Using this commonality I would refactor adding an item to a separate function.

    def addItem(item, name, offset):           if isinstance(item, dict):                 lines.append(offset + u'<%s>' % name)                 addDict(item, offset + u' ' * 4)                 lines.append(offset + u'</%s>' % name)           else:                 lines.append(offset + u'<%s>%s</%s>' % (name, escape(unicodify(item)), name))  def addList(value,name, offset):         for item in value:             addItem(item, name, offset)  def addDict(node, offset):         for name, value in node.iteritems():             if isinstance(value, list):                 addList(value, name, offset)             else:                 addItem(value, name, offset) 

    Advisory warning: this code is not tested or written by anybody who actually uses Python.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have used Ninject quite a bit over the last… May 11, 2026 at 8:18 pm
  • Editorial Team
    Editorial Team added an answer The basic mod_access module should get you what you need… May 11, 2026 at 8:18 pm
  • Editorial Team
    Editorial Team added an answer Raising of signal number 6 (SIGABRT in Linux) is one… May 11, 2026 at 8:18 pm

Related Questions

Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
It seems to me obfuscation is an idea that falls somewhere in the security

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.