-1

Possible Duplicate:
Converting XML to JSON using Python?

I am importing an XML feed and trying to convert it to JSON for output. I'm getting this error:

TypeError: <xml.dom.minidom.Document instance at 0x72787d8> is not JSON serializable

Unfortunately I know next to nothing about Python. I'm developing this on the Google App Engine. I could use some help, because my little 2 hour hack that was going so well is now on its 3rd day.

XML data:

<?xml version="1.0" ?><eveapi version="2">
  <currentTime>2009-01-25 15:03:27</currentTime>
  <result>
    <rowset columns="name,characterID,corporationName,corporationID" key="characterID" name="characters">
      <row characterID="999999" corporationID="999999" corporationName="filler data" name="someName"/>
    </rowset>
  </result>
  <cachedUntil>2009-01-25 15:04:55</cachedUntil>

</eveapi>

My code:

class doproxy(webapp.RequestHandler):
def get(self):
    apiurl = 'http://api.eve-online.com'

    path = self.request.get('path');
    type = self.request.get('type');
    args = '&'+self.request.get('args');

    #assemble api url
    url = apiurl+path

    #do GET request     
    if type == 'get':
        result = urlfetch.fetch(url,'','get');

    #do POST request
    if type == 'post':
        result = urlfetch.fetch(url,args,'post');   

    if result.status_code == 200:               
        dom = minidom.parseString( result.content ) #.encode( "utf-8" ) )
        dom2json = simplejson.dump(dom,"utf-8")
Community
  • 1
  • 1
Geuis
  • 41,122
  • 56
  • 157
  • 219
  • Why was this down voted? Yeah the OP is definitely new to Python and coding but the fastest way to learn is to ask questions like this. – David Jan 25 '09 at 15:57
  • I believe the way the question is written caused the downvoting. – nosklo Jan 25 '09 at 16:50
  • probably down voted because he said python is not documented... – Nope Jan 25 '09 at 17:08
  • Question isn't well written but it's a decent one. I've edited. Let's remove some downvotes -this is probably useful to other people. – Kenan Banks Jan 25 '09 at 17:59
  • Why do you ask about the same (identical title) thing you asked 2 days ago at http://stackoverflow.com/questions/471946/ ? – Piotr Dobrogost Oct 15 '12 at 17:15
  • Piotr, this question is over 3 years old. I asked it when I was new to Python. I was also relatively new to the site at the time too. If you look at my profile, I've progressed quite a bit since then. =) In retrospect, this question was appropriately closed at the time. – Geuis Oct 16 '12 at 00:59

1 Answers1

9

I'm quickly coming to the opinion that Python is potentially a great language, but that none of its users know how to actually document anything in a clear and concise way.

The attitude of the question isn't going to help with getting answers from these same Python users.

As is mentioned in the answers to this related question, there is no 1-to-1 correspondence between XML and JSON so the conversion can't be done automatically.

In the documentation for simplejson you can find the list of types that it's able to serialize, which are basically the native Python types (dict, list, unicode, int, float, True/False, None).

So, you have to create a Python data structure containing only these types, which you will then give to simplejson.dump().

Community
  • 1
  • 1
dF.
  • 74,139
  • 30
  • 130
  • 136
  • @Geuis I recommend you pick up an introduction to Computer science book, your going to be banging your head against a wall until you start getting the big picture of data structures and software design. – David Jan 25 '09 at 15:59