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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:22:00+00:00 2026-05-14T02:22:00+00:00

Possible Duplicate: Converting XML to JSON using Python? I’m doing some work on App

  • 0

Possible Duplicate:
Converting XML to JSON using Python?

I’m doing some work on App Engine and I need to convert an XML document being retrieved from a remote server into an equivalent JSON object.

I’m using xml.dom.minidom to parse the XML data being returned by urlfetch. I’m also trying to use django.utils.simplejson to convert the parsed XML document into JSON. I’m completely at a loss as to how to hook the two together. Below is the code I’m tinkering with:

from xml.dom import minidom
from django.utils import simplejson as json

#pseudo code that returns actual xml data as a string from remote server. 
result = urlfetch.fetch(url,'','get');

dom = minidom.parseString(result.content)
json = simplejson.load(dom)

self.response.out.write(json)
  • 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-14T02:22:01+00:00Added an answer on May 14, 2026 at 2:22 am

    Soviut’s advice for lxml objectify is good. With a specially subclassed simplejson, you can turn an lxml objectify result into json.

    import simplejson as json
    import lxml
    
    class objectJSONEncoder(json.JSONEncoder):
      """A specialized JSON encoder that can handle simple lxml objectify types
          >>> from lxml import objectify
          >>> obj = objectify.fromstring("<Book><price>1.50</price><author>W. Shakespeare</author></Book>")       
          >>> objectJSONEncoder().encode(obj)
          '{"price": 1.5, "author": "W. Shakespeare"}'       
     """
    
    
        def default(self,o):
            if isinstance(o, lxml.objectify.IntElement):
                return int(o)
            if isinstance(o, lxml.objectify.NumberElement) or isinstance(o, lxml.objectify.FloatElement):
                return float(o)
            if isinstance(o, lxml.objectify.ObjectifiedDataElement):
                return str(o)
            if hasattr(o, '__dict__'):
                #For objects with a __dict__, return the encoding of the __dict__
                return o.__dict__
            return json.JSONEncoder.default(self, o)
    

    See the docstring for example of usage, essentially you pass the result of lxml objectify to the encode method of an instance of objectJSONEncoder

    Note that Koen’s point is very valid here, the solution above only works for simply nested xml and doesn’t include the name of root elements. This could be fixed.

    I’ve included this class in a gist here: http://gist.github.com/345559

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

Sidebar

Related Questions

Possible Duplicate: Converting XML to JSON using Python? I am importing an XML feed
Possible Duplicate: Converting .NET DateTime to JSON How can I convert a date value
Possible Duplicate: Converting input xml using xslt to other XML I am astarter in
Possible Duplicate: Alternative to itoa() for converting integer to string C++? How to convert
Possible Duplicate: Converting ereg expressions to preg I'm trying to fix some old code
Possible Duplicate: Converting a four character string to a long I want to convert
Possible Duplicate: converting pixels to dp in android I'm trying to convert pixels to
Possible Duplicate: Byte array in objective-c Am converting some Java code to Objective-C and
Possible Duplicate: Converting a PDF to a series of images with Python I know
Possible Duplicate: Converting unix timestamp string to readable date in Python I have a

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.