清单 3: try_sax.py
"Simple SAX example, updated for Python 2.0+"
import
string
import
xml.sax
from
xml.sax.handler
import
*
class
QuotationHandler
(ContentHandler):
"""Crude extractor for quotations.dtd compliant XML document"""
__init__
(self):
self.in_quote = 0
self.thisquote = ''
startDocument
(self):
print
'--- Begin Document ---'
startElement
(self, name, attrs):
if
name == 'quotation':
print
'QUOTATION:'
self.in_quote = 1
else:
self.thisquote = self.thisquote + '{'
endElement
(self, name):
if
name == 'quotation':
print
string.join(string.split(self.thisquote[:230]))+'...',
print
'('+str(len(self.thisquote))+' bytes)
'
self.thisquote = ''
self.in_quote = 0
else:
self.thisquote = self.thisquote + '}'
characters
(self, ch):
if
self.in_quote:
self.thisquote = self.thisquote + ch
if
__name__ == '__main__':
parser = xml.sax.make_parser()
handler = QuotationHandler()
parser.setContentHandler(handler)
parser.parse("sample.xml")文章地址: http://www.qqread.com/xml/j350687.html 相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- 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次浏览)



