XML parsing in GAE python

131 views
Skip to first unread message

Ankur Deshwal

unread,
Sep 8, 2011, 8:40:34 AM9/8/11
to Google App Engine
Hi all,

I am trying to send data to a GAE app in xml format via http POST
request. The GAE application is supposed to parse it.

Code handling POST request in GAE is

class Guestbook(webapp.RequestHandler):
def post(self):
global val_list
guestbook_name = self.request.get('guestbook_name')
greeting = Greeting(parent=guestbook_key(guestbook_name))

sensor_val = self.request.get('content')
greeting.content = sensor_val
greeting.put()

Code for displaying parsed value on browser is

class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
guestbook_name=self.request.get('guestbook_name')

greetings = db.GqlQuery("SELECT * "
"FROM Greeting "
"WHERE ANCESTOR IS :1 "
"ORDER BY date DESC LIMIT 10",
guestbook_key(guestbook_name))
for greeting in greetings:
val = cgi.escape(greeting.content)
dom_val = minidom.parseString(val)
val_pretty = dom_val.toprettyxml()
self.response.out.write('<blockquote>%s</blockquote>'
%val_pretty)

The problem is, when I use a string directly in above code as
dom_val = minidom.parseString('<eg>example text</eg>')

The code parses the xml string successfully. However when I send the
string from a client application as


h = httplib2.Http()
form_fields = {
'content': '<eg>example text</eg>'
}
data = urllib.urlencode(form_fields)

resp, content = h.request('http://107.108.58.183:8080/sign',
'POST',
data ,
headers={'Content-Type': 'application/x-www-form-urlencoded'})

Following error happens while parsing

Traceback (most recent call last):
File "/home/asd/google_app_engine/google_appengine/google/appengine/
ext/webapp/__init__.py", line 700, in __call__
handler.get(*groups)
File "/home/asd/google_app_engine/projects/sensor_xml/
helloworld.py", line 44, in get
dom_val = minidom.parseString(val)
File "/usr/lib/python2.7/xml/dom/minidom.py", line 1924, in
parseString
return expatbuilder.parseString(string)
File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 940, in
parseString
return builder.parseString(string)
File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 223, in
parseString
parser.Parse(string, True)
ExpatError: not well-formed (invalid token): line 1, column 0


The string is received by GAE as I am able to print it by printing
variable var successfully. however parsing it as xml generates the
error.

Please help.


Tim Hoffman

unread,
Sep 8, 2011, 10:51:28 AM9/8/11
to google-a...@googlegroups.com
Hi

Did you know the dev SDK still doesn't support python 2.7.
(Will soon but not right now)

Have you tried running the same code on python 2.5 ?

Though its probably not the problem.

Have you actually looked at the value of val from your line
dom_val = minidom.parseString(val) 

To see if it is well formed xml 

Rgds

Tim

voscausa

unread,
Sep 8, 2011, 11:16:12 AM9/8/11
to google-a...@googlegroups.com
Maybe something is wrong with the encoding. Try this:
dom_val = minidom.parseString(val.encode('utf-8'))
Reply all
Reply to author
Forward
0 new messages