I'm trying out suds (0.4 trunk) and getting a weird behavior with the result. If I pass the result of a call into a sax parser, I get a 'not well formed (invalid token)' error. If I save that same result to a file, then pass that file to the parser, it works. If I read that string from the file, and pass it to the parser, it works.
Any ideas?
Here's a short script showing the problem.
#!/usr/bin/python
import os import suds
url = 'http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl' client = suds.client.Client(url)
# print client
result = client.service.NDFDgenByDay(39.0000, -77.0000, 2004-04-27, 1, '12 hourly')
print 'Result:\n', result
try: p = suds.sax.parser.Parser() print 'From string:', p.parse(string=result) except Exception, e: print 'Oops:', e
open('nws_out.xml', 'w').write(result)
print 'From file:', repr(p.parse(file=open('nws_out.xml'))) print 'From file:', repr(p.parse(string=open('nws_out.xml').read()))
On 03/02/2010 04:45 PM, Joshua J. Kugler wrote:
I'm trying out suds (0.4 trunk) and getting a weird behavior with the result. If I pass the result of a call into a sax parser, I get a 'not well formed (invalid token)' error. If I save that same result to a file, then pass that file to the parser, it works. If I read that string from the file, and pass it to the parser, it works.
Any ideas?
Here's a short script showing the problem.
#!/usr/bin/python
import os import suds
url = 'http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl' client = suds.client.Client(url)
# print client
result = client.service.NDFDgenByDay(39.0000, -77.0000, 2004-04-27, 1, '12 hourly')
"result" is actually a class of type suds.sax.text.Text which is a subclass of unicode. Seems that either the StringIO or expat parser has a problem unicode. Probably the StringIO. The reason it works to write to a file and then read it again is that when you read(), you now have a str object. When reading directly from a file, the parser is reading a file-like object.
print 'Result:\n', result
try: p = suds.sax.parser.Parser() print 'From string:', p.parse(string=result)
So, this:
print 'From string:', p.parse(string=str(result))
works and this:
print 'From string:', p.parse(string=unicode(result))
does not.
I'll look into this further.
except Exception, e: print 'Oops:', e
open('nws_out.xml', 'w').write(result)
print 'From file:', repr(p.parse(file=open('nws_out.xml'))) print 'From file:', repr(p.parse(string=open('nws_out.xml').read()))
On Tuesday 02 March 2010, Jeff Ortel elucidated thus:
"result" is actually a class of type suds.sax.text.Text which is a subclass of unicode. Seems that either the StringIO or expat parser has a problem unicode. Probably the StringIO. The reason it works to write to a file and then read it again is that when you read(),
Ah, that make sense. I wasn't catching on to the fact that it was unicode.
you now have a str object. When reading directly from a file, the parser is reading a file-like object.
Figured it was something like that, but since 'print result' worked, I wasn't catching the unicode glitch. :)
So, this:
print 'From string:', p.parse(string=str(result))
works and this:
print 'From string:', p.parse(string=unicode(result))
Thanks for the work around. That will help!
j