xml.sax problem: getting parse() to read a string

S

sturnfie

Hey all, I recently came across the xml.sax libraries and am trying to
use them. I am currently making a string variable, and am attempting
to pass it into a parser instance as follows:

def parseMessage(self, message):
#create a XML parser
parser = make_parser()

#create an instance of our handler class
#generic, prints out to screen on all events
dh = docHandler()

#tell parser to use our handler
parser.setContentHandler(dh)

#start it going, will trigger on events as defined in the
docHandler class
parser.parse(message)
return

"message" is the following text:
-----------------
<?xml version="1.0" ?>
- <ENVELOPE>
- <MESSAGE>
<timestamp>timestamp</timestamp>
- <COMMAND>
<SCOPE>asdasd</SCOPE>
<ACTION>asdasds</ACTION>
</COMMAND>
</MESSAGE>
</ENVELOPE>
---------------


This is dying with the following errors.

File "...../python/lib/python2.4/urllib.py", line 77, in urlopen
return opener.open(url)
File "...../python/lib/python2.4/urllib.py", line 180, in open
return getattr(self, name)(url)
File "...../python/lib/python2.4/urllib.py", line 409, in open_file
return self.open_local_file(url)
File "...../python/lib/python2.4/urllib.py", line 419, in
open_local_file
raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] No such file or directory: '?xml version="1.0" ?>\n-
<ENVELOPE>\n - <MESSAGE>\n <timestamp>timestamp</timestamp>\n
- <COMMAND>\n <SCOPE>asdasd</SCOPE>\n
<ACTION>asdasds</ACTION>\n </COMMAND>\n </MESSAGE>\n
</ENVELOPE'



So in recap, it looks like it is trying to take my string argument as a
file handler. How can I get around this? I would prefer not to make a
bigger footprint on the system by creating a file......


Thanks in advance!
 
F

Fredrik Lundh

So in recap, it looks like it is trying to take my string argument as a
file handler. How can I get around this?

if you want to parse a string, use xml.sax.parseString instead of
xml.sax.parse.

</F>
 
S

sturnfie

Fredrik said:
if you want to parse a string, use xml.sax.parseString instead of
xml.sax.parse.

</F>

My function has changed to the following (parseString call instead of
parse):

def parseMessage(self, message):
#create a XML parser
parser = make_parser()

#create an instance of our handler class
#generic, prints out to screen on all events
dh = docHandler()

#tell parser to use our handler
parser.setContentHandler(dh)

parser.parseString(message)
return


I am getting the following error.

File "acmtest.py", line 205, in parseMessage
parser.parseString(message)
AttributeError: ExpatParser instance has no attribute 'parseString'

Am I simply missing that library here? Or am I calling it incorrectly?
My import line reads as follows (but I am not sure how to explictly
make sure I have this library)

import socket, select, os, sys, traceback, re
from xml.sax import make_parser, parseString
from xml.sax.handler import ContentHandler
 
F

Fredrik Lundh

I am getting the following error.

File "acmtest.py", line 205, in parseMessage
parser.parseString(message)
AttributeError: ExpatParser instance has no attribute 'parseString'

Am I simply missing that library here? Or am I calling it incorrectly?

as mentioned in the documentation, and implied by my answer, parseString
is a helper function in the xml.sax module, not a parser method. try doing

xml.sax.parseString(string, handler)

instead of that make_parser/setContentHandler/parse dance.

</F>
 
S

sturnfie

Fredrik said:
as mentioned in the documentation, and implied by my answer, parseString
is a helper function in the xml.sax module, not a parser method. try doing

xml.sax.parseString(string, handler)

instead of that make_parser/setContentHandler/parse dance.

</F>

Thanks a ton for all your help! I have successfully moved past this
obstacle, with much appreciated thanks to your postings here (and other
postings of yours I found via google).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top