def test_parse():
import os
filename = "/Users/dalke/Music/iTunes/iTunes Music Library.xml"
if not os.path.exists(filename):
print "Cannot find %r: skipping test" % (filename,)
return
# Work through callbacks
ef = IterParseFilter()
def print_info(event, ele, state):
d = {}
children = iter(ele)
for child in children:
key = child.text
value = children.next().text
d[key] = value
print "%r is by %r" % (d["Name"], d.get("Artist", "<unknown>"))
ele.clear()
ef.on_end("/plist/dict/dict/dict", print_info)
ef.handler_parse(open(filename))
# Work through iterators
ef = IterParseFilter()
ef.iter_end("/plist/dict/dict/dict")
for (event, ele) in ef.iterparse(open(filename)):
d = {}
children = iter(ele)
for child in children:
key = child.text
value = children.next().text
d[key] = value
print "%r is a %r song" % (d["Name"], d.get("Genre", "<unknown>"))
ele.clear()
评论列表
文章目录