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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:14:59+00:00 2026-05-13T17:14:59+00:00

I’m learning python for the first time. I have an aim which is to

  • 0

I’m learning python for the first time.
I have an aim which is to take data from an API and output it as xml.

The output is stored in an array (“projectData”), here is an example of the output:

[{'code': 'demo',
 'created_at': datetime.datetime(2008, 6, 11, 7, 35, 19, tzinfo=<api.LocalTimezone object at 0x10072ab10>),
 'created_by': None,
 'id': 4,
 'image': 'https://website.com/files/0000/0000/blah.jpg',
 'name': 'Demo Project',
 'description': 'This is for demonstration purposes',
 'due': '2009-05-30',
 'start': '2009-05-06',
 'status': 'Active',
 'stype': 'Demo',
 'tag_list': [],
 'type': 'Project',
 'updated_at': datetime.datetime(2009, 5, 27, 1, 41, 55, tzinfo=<api.LocalTimezone object at 0x10072ab10>),
 'updated_by': {'id': 24, 'name': 'Test', 'type': 'HumanUser'},
 'users': [{'id': 19, 'name': 'User 1', 'type': 'HumanUser'},
           {'id': 18, 'name': 'User 2', 'type': 'HumanUser'},
           {'id': 17, 'name': 'User 3', 'type': 'HumanUser'},
           {'id': 16, 'name': 'User 4', 'type': 'HumanUser'},
           {'id': 15, 'name': 'User 5', 'type': 'HumanUser'},
           {'id': 14, 'name': 'User 6', 'type': 'HumanUser'},
           {'id': 13, 'name': 'User 7', 'type': 'HumanUser'},
           {'id': 12, 'name': 'User 8', 'type': 'HumanUser'},
           {'id': 20, 'name': 'Client 1', 'type': 'HumanUser'}]},

(etc.)

I’ve written some code which will output it as xml like so:

for _project in projectData:
  print "<Project>"
  for key in _project:
    value = _project[key]
    print "\t<" + str(key) + ">" + str(value) + "</" + str(key) + ">"
  print("</Project>\n")

Which actually gives me a result that works for me.

However, because I’m new to this, I suspect that this isn’t a very efficient approach, and may be susceptible to all sorts of bugs, I was hoping that someone more knowledgeable might have some pointers for me. The next thing I want to try with this is make it recursive, so that the “updated_by” element for example returns its own xml

Thanks.

  • 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-05-13T17:14:59+00:00Added an answer on May 13, 2026 at 5:14 pm

    Here’s an example using lxml.etree, incomplete.. and probably a bit naive. Really you should define a schema and make sure your output is consistent with it.

    Edit, said it was incomplete, added None type and assumed a created_by is like an updated_by when populated

    import datetime
    
    projects = [{'code': 'demo',
     'created_at': datetime.datetime(2008, 6, 11, 7, 35, 19),
     'created_by': None,
     'id': 4,
     'image': 'https://website.com/files/0000/0000/blah.jpg',
     'name': 'Demo Project',
     'description': 'This is for demonstration purposes',
     'due': '2009-05-30',
     'start': '2009-05-06',
     'status': 'Active',
     'stype': 'Demo',
     'tag_list': [],
     'type': 'Project',
     'updated_at': datetime.datetime(2009, 5, 27, 1, 41, 55),
     'updated_by': {'id': 24, 'name': 'Test', 'type': 'HumanUser'},
     'users': [{'id': 19, 'name': 'User 1', 'type': 'HumanUser'},
               {'id': 18, 'name': 'User 2', 'type': 'HumanUser'},
               {'id': 17, 'name': 'User 3', 'type': 'HumanUser'},
               {'id': 16, 'name': 'User 4', 'type': 'HumanUser'},
               {'id': 15, 'name': 'User 5', 'type': 'HumanUser'},
               {'id': 14, 'name': 'User 6', 'type': 'HumanUser'},
               {'id': 13, 'name': 'User 7', 'type': 'HumanUser'},
               {'id': 12, 'name': 'User 8', 'type': 'HumanUser'},
               {'id': 20, 'name': 'Client 1', 'type': 'HumanUser'}]},
     ]
    
    from lxml import etree
    
    def E(tag, parent=None, content=None, children=None, **kw):
      e = etree.Element(tag)
      if not content is None:
        e.text = str(content)
      for k,v in kw.items():
        e.set(k, str(v))
      if not parent is None:
        parent.append(e)
      if not children is None:
        for c in children:
          e.append(c)
      return e
    
    def processProject(data):
      attrs = ('name','type','id')
      p = E('Project')
      for item in attrs:
        p.set(item,str(data[item]))
      for k,v in [ x for x in data.items() if x[0] not in attrs ]:
        if v is None:
          E(k,parent=p)
        elif isinstance(v,basestring):
          E(k,content=v,parent=p)
        elif isinstance(v,(float,long,int)):
          E(k,content=str(v),parent=p)
        elif isinstance(v,datetime.datetime):
          E(k,content=v.strftime('%Y-%m-%d %H%M'),parent=p)
        elif k == 'users':
          users = E(k,parent=p)
          for u in v:
            E('user',parent=users,**dict([ (x,str(y)) for (x,y) in u.items()]))
        elif k in ('updated_by','created_by'):
          E(k,parent=p,**dict([ (x,str(y)) for (x,y) in v.items()]))
        elif k == 'tag_list':
          taglist = E(k,parent=p)
          for t in v:
            E('tag',parent=taglist,content=t)
      return p
    
    >>> projxml = processProject(projects[0])
    >>> etree.dump(projxml)
    <Project name="Demo Project" type="Project" id="4">
      <status>Active</status>
      <code>demo</code>
      <created_at>2008-06-11 0735</created_at>
      <due>2009-05-30</due>
      <created_by/>
      <updated_at>2009-05-27 0141</updated_at>
      <start>2009-05-06</start>
      <image>https://website.com/files/0000/0000/blah.jpg</image>
      <updated_by type="HumanUser" id="24" name="Test"/>
      <users>
        <user type="HumanUser" id="19" name="User 1"/>
        <user type="HumanUser" id="18" name="User 2"/>
        <user type="HumanUser" id="17" name="User 3"/>
        <user type="HumanUser" id="16" name="User 4"/>
        <user type="HumanUser" id="15" name="User 5"/>
        <user type="HumanUser" id="14" name="User 6"/>
        <user type="HumanUser" id="13" name="User 7"/>
        <user type="HumanUser" id="12" name="User 8"/>
        <user type="HumanUser" id="20" name="Client 1"/>
      </users>
      <tag_list/>
      <stype>Demo</stype>
      <description>This is for demonstration purposes</description>
    </Project>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 386k
  • Answers 386k
  • 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 jQuery: $.post('url_to_script',{"anyData": "that is needed"}, function(data){ $('#headertarget').text('data'); }); PHP: $anyData… May 14, 2026 at 11:48 pm
  • Editorial Team
    Editorial Team added an answer If you install the SDK, the offline documentation can be… May 14, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer You can't switch on a boolean (which only have 2… May 14, 2026 at 11:47 pm

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.