Hi everyone,<br><br>I have another question I'm hoping someone would be kind enough to answer. I am new to parsing XML (not to mention much of Python itself) and I am trying to parse an XML file. The file I am trying to parse is this one: <a href="http://ws.audioscrobbler.com/2.0/user/bryansmith/topalbums.xml">http://ws.audioscrobbler.com/2.0/user/bryansmith/topalbums.xml</a>.<br>
<br>So far, I have written up a class for parsing this file in my attempts to present to the user a list of top albums on their <a href="http://last.fm">last.fm</a> profile. If you note, the artist name and album name are both signified by the <name> tag which makes my job harder. If the tag names were different, I wouldn't have a problem. Listed below is the class I have written to parse the file. My question then is this: is there a way I can say something like "if tag_name == album name tag then....elif tag_name == artist name tag....". I hope this is clear.<br>
<br>As it stands right now, if I parse this file and print the results, this is what I get (understandably) if I try to print out in the following fashion - album (playcount): Vheissu (332), Thrice (289), The Artist in the Ambulance (286), Thrice (210) and so on. Thrice is the artist name. I want to be able to differentiate between the "artist" name tag and the "album" name tag.<br>
<br><br>Class as it stands right now:<br><br>class GetTopAlbums(ContentHandler):<br> <br> in_album_tag = False<br> in_playcount_tag = False<br> <br> def __init__(self, album, playcount):<br> ContentHandler.__init__(self)<br>
self.album = album<br> self.playcount = playcount<br> self.data = []<br> <br> def startElement(self, tag_name, attr):<br> if tag_name == "name":<br> self.in_album_tag = True<br>
elif tag_name == "playcount":<br> self.in_playcount_tag = True<br> <br> def endElement(self, tag_name):<br> if tag_name == "name":<br> content = "".join(self.data)<br>
self.data = []<br> self.album.append(content)<br> self.in_album_tag = False<br> elif tag_name == "playcount":<br> content = "".join(self.data)<br> self.data = []<br>
self.playcount.append(content)<br> self.in_playcount_tag = False<br> <br> def characters(self, string):<br> if self.in_album_tag == True:<br> self.data.append(string)<br>
elif self.in_playcount_tag == True:<br> self.data.append(string)<br><br>Thanks in advance!<br>Bryan<br>