2010年12月31日 星期五

使用SAX讀取XML文件

如何使用:test.py
主程式:test.py
說明:test.py讀取books.xml後,將結果輸出至螢幕上

books.xml




The Cathedral & the Bazaar
Eric S. Raymond


Making TeX Work
Norman Walsh



bookhandler.py程式碼


import xml.sax.handler

class BookHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.inTitle = 0
self.mapping = {}

def startElement(self, name, attributes):
if name == "book":
self.buffer = ""
self.isbn = attributes["isbn"]
elif name == "title":
self.inTitle = 1

def characters(self, data):
if self.inTitle:
self.buffer += data

def endElement(self, name):
if name == "title":
self.inTitle = 0
self.mapping[self.isbn] = self.buffer

test.py程式碼


import xml.sax
import bookhandler
import pprint

parser = xml.sax.make_parser()
handler = bookhandler.BookHandler()
parser.setContentHandler(handler)
parser.parse("books.xml")
pprint.pprint(handler.mapping)

輸出
{u'1-56592-051-1': u'Making TeX Work',
u'1-56592-724-9': u'The Cathedral & the Bazaar'}

沒有留言: