2

Hey. I tried to find a way but i can't. I have set up a xml.sax parser in python and it works perfect when i read a local file (for example calendar.xml), but i need to read a xml file from a web address.

I figured it would work if i do this:

toursxml='http://api.songkick.com/api/3.0/artists/mbid:'+mbid+'/calendar.xml?apikey=---------'
toursurl=urllib2.urlopen(toursxml)
toursurl=toursurl.read()
parser.parse(toursurl)

but it doesnt. im sure theres an easy way but i cant find it.

so yeah I can easily go to the url and download the file and open it by doing

parser.parse("calendar.xml")

as a work around ive set it up to read the file and create the file locally, close the file, and then read it. But as you can guess its slow as hell.

Is there anyone to directly read the xml? also note that the url name does not end in ".xml" so that may be a problem later

1
  • First, please try to be more precise: use Capital Letters where appropriate. Also, read the API documents very, very carefully. Commented Mar 17, 2011 at 22:15

2 Answers 2

3

First, your example is mixed up. Please don't reuse variables.

toursurl= urllib2.urlopen(toursxml)
toursurl_string= toursurl.read()
parser.parseString( toursurl_string )

Reads the entire file into a string, named toursurl_string.

To parse a string, you use the parseString(toursurl_string) method.

http://docs.python.org/library/xml.sax.html#xml.sax.parseString

If you want to combine reading and parsing, you have to pass the "stream" or filename to parse.

toursurl= urllib2.urlopen(toursxml)
parser.parse(toursurl)
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to parse from the http stream without having a huge string in memory?
2
parser.parse(xyz)

expects xyz to be a file; you are looking for

parser.parseString(xyz)

which expects xyz to be a string containing XML.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.