
startDocument() 方法
文档启动的之后调用。
endDocument() 方法
解析器到达文档结尾时读取。
startElement(name, attrs)方法

遇到XML开始标签时调用解析xml,name是标签的昵称,attrs是标签的属性值字典。
endElement(name) 方法
遇到XML结束标签时调用。
以下方式建立一个新的解析器对象并返回。
xml.sax.make_parser( [parser_list] )

参数说明:
以下方法构建一个 SAX 解析器并解析xml文档:
xml.sax.parse( xmlfile, contenthandler[, errorhandler])
参数说明:
parseString方法建立一个XML解析器并解析xml字符串:

xml.sax.parseString(xmlstring, contenthandler[, errorhandler])
参数说明:
以上代码执行结果如下:
*****Movie***** Title: Enemy Behind Type: War, Thriller Format: DVD Year: 2003 Rating: PG Stars: 10 Description: Talk about a US-Japan war *****Movie***** Title: Transformers Type: Anime, Science Fiction Format: DVD Year: 1989 Rating: R Stars: 8 Description: A schientific fiction *****Movie***** Title: Trigun Type: Anime, Action Format: DVD Rating: PG Stars: 10 Description: Vash the Stampede! *****Movie***** Title: Ishtar Type: Comedy Format: VHS Rating: PG Stars: 2 Description: Viewable boredom
完整的 SAX API 文档请查阅Python SAX APIs

文件对象模型(Document Object Model解析xml,简称DOM),是W3C组织推荐的处理可扩展置标语言的标准编程接口。
一个 DOM 的解析器在解析一个 XML 文档时,一次性读取整个文档,把文档中所有元素储存在存储中的一个树结构里,之后你可以利用DOM 提供的不同的变量来调用或更改文档的内容跟结构,也可以把设置过的内容写入xml文件。
python中用xml.dom.minidom来解读xml文件,实例如下:
以上程序执行结果如下:
Root element : New Arrivals *****Movie***** Title: Enemy Behind Type: War, Thriller Format: DVD Rating: PG Description: Talk about a US-Japan war *****Movie***** Title: Transformers Type: Anime, Science Fiction Format: DVD Rating: R Description: A schientific fiction *****Movie***** Title: Trigun Type: Anime, Action Format: DVD Rating: PG Description: Vash the Stampede! *****Movie***** Title: Ishtar Type: Comedy Format: VHS Rating: PG Description: Viewable boredom
完整的 DOM API 文档请查阅Python DOM APIs。
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-140226-1.html
海南岛边12海里
再次减持美国国债