472,958 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 5839
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: tyousaf | last post by:
Hi i am new to mysql and mysql++, i have installed mysql server, it is running fine. i also installed "mysql++-1.7.9gcc3.2-2.i386.rpm" (i have gcc 3.3) , first of all as the readme file says to do...
4
by: Zephyr . via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Zephyr . hey, i got trouble getting integers out of a plain text file. i dont want to use binary files, just plain text files...
3
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are...
4
by: Paul Bromley | last post by:
I thought that XMLTextReader would be simple to use, but I have run into problems with it! I seem to have great difficulty extrcting the text of specific elements from a very simple XML file. I...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
5
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I...
7
by: souravmallik | last post by:
Hello, I'm facing a big logical problem while writing a parser in VC++ using C. I have to parse a file in a chunk of bytes in a round robin fashion. Means, when I select a file, the parser...
2
by: hgarg | last post by:
The onpaint() is getting called before calculating the required parameters by listBox1_SelectedIndexChanged function. I tried calling it at the end of this function. But of no use. Due to this issue...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.