同じことを行うpythonスクリプトは次のとおりです。つまり、Rhythmboxが使用するxmlデータベースからインターネットラジオ局の名前と場所を抽出します。
import xml.sax.handler
import xml.sax
import pprint
class RhythmboxPlaylistHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.inRTitle = False
self.inRLocation = False
self.entrytype = "undefined"
self.titlebuffer = ""
self.locationbuffer = ""
self.radiostations = {}
def startElement(self, name, attributes):
if name == "entry":
self.entrytype = attributes["type"] # we're interested in type="iradio"
elif name == "title" and self.entrytype == "iradio":
self.inRTitle = True
elif name == "location" and self.entrytype == "iradio":
self.inRLocation = True
def characters(self, data):
if self.inRTitle:
self.titlebuffer += data
elif self.inRLocation:
self.locationbuffer += data
def endElement(self, name):
if name == "title":
self.inRTitle = False
elif name == "location":
self.inRLocation = False
elif name == "entry" and self.entrytype == "iradio":
self.radiostations[self.titlebuffer] = self.locationbuffer
self.titlebuffer=""
self.locationbuffer=""
parser = xml.sax.make_parser( )
handler = RhythmboxPlaylistHandler( )
parser.setContentHandler(handler)
parser.parse("work_copy_of_rhythmdb.xml")
pprint.pprint(handler.radiostations)
rstations=handler.radiostations
rskeys=[key for key in rstations]
rskeys.sort()
ofile=open("rhytmbox_current_internet_radiostations.txt","w")
ofile.write("# {0:41} --> {1}\r\n".format('radio station name','location'))
ofile.write("#"+120*'-'+"\r\n")
for key in rskeys:
ofile.write("{0:45} --> {1}\r\n".format(key,rstations[key]))
ofile.close()
(Python内からXMLデータベースを操作するこのチュートリアルから始めました:http : //oreilly.com/catalog/pythonxml/chapter/ch01.html)