清单 1: try_xmllib.pyimport
xmllib, string
class
QuotationParser
(xmllib.XMLParser):
"""Crude xmllib extractor for quotations.dtd document"""
__init__
(self):
xmllib.XMLParser.__init__(self)
self.thisquote = '' # quotation accumulator
def
handle_data
(self, data):
self.thisquote = self.thisquote + data
syntax_error
(self, message):
pass
def
start_quotations
(self, attrs): # top level tag
print
'--- Begin Document ---'
start_quotation
(self, attrs):
print
'QUOTATION:'
end_quotation
(self):
print
string.join(string.split(self.thisquote[:230]))+'...',
print
'('+str(len(self.thisquote))+' bytes)
'
self.thisquote = ''
unknown_starttag
(self, tag, attrs):
self.thisquote = self.thisquote + '{'
unknown_endtag
(self, tag):
self.thisquote = self.thisquote + '}'
unknown_charref
(self, ref):
self.thisquote = self.thisquote + '?'
unknown_entityref
(self, ref):
self.thisquote = self.thisquote + '#'
if
__name__ == '__main__':
parser = QuotationParser()
for
c
in
open("sample.xml").read():
parser.feed(c)
parser.close()
相关专题
- Python实用指南 (220篇文章)
- XML详解 (1550篇文章)
- Python相关文章 (220篇文章)
- Python编程 (220篇文章)
- XML基础教程 (852次浏览)
- XML简介 (714次浏览)
- 如何读取XML文件内容 (561次浏览)
- XML与面向Web的数据挖掘技术 (544次浏览)
- 了解 XML实现通用的数据访问 (482次浏览)
- XML入门精解之文件格式定义(DTD) (376次浏览)
- XML的简单读取与写入 (339次浏览)
- DOM的结构 (329次浏览)
- XML入门之11问答 (323次浏览)
- XMLHTTP资料 (289次浏览)



