470,818 Members | 1,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,818 developers. It's quick & easy.

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

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!
--
lucas

Jun 5 '06 #1
4 5641
st******@gmail.com wrote:
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>

Jun 5 '06 #2

Fredrik Lundh wrote:
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

--
lucas

Jun 5 '06 #3
st******@gmail.com wrote:
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>

Jun 5 '06 #4

Fredrik Lundh wrote:
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).

--
lucas

Jun 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by tyousaf | last post: by
4 posts views Thread by Zephyr . via .NET 247 | last post: by
4 posts views Thread by Paul Bromley | last post: by
2 posts views Thread by Mike Collins | last post: by
5 posts views Thread by Alex Mathieu | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.