動作確認
# -*- coding:utf-8 -*-
import lxml.etree
tree = lxml.etree.parse('test01.xml')
root = tree.getroot()
print root.tag
foods = root.iterfind('food')
print foods
for food in foods:
print food
children = food.getchildren()
for c in children:
print c.tag
name = food.find('name')
print "tag: ", name.tag
print "text: ", name.text
実行結果
> python test03.py
root.tag: breakfast_menu
foods:
food:
child.tag: name
child.tag: price
child.tag: description
child.tag: calories
tag: name
text: Belgian Waffles
food:
child.tag: name
child.tag: price
child.tag: description
child.tag: calories
tag: name
text: Strawberry Belgian Waffles
food:
child.tag: name
child.tag: price
child.tag: description
child.tag: calories
tag: name
text: Berry-Berry Belgian Waffles
food:
child.tag: name
child.tag: price
child.tag: description
child.tag: calories
tag: name
text: French Toast
food:
child.tag: name
child.tag: price
child.tag: description
child.tag: calories
tag: name
text: Homestyle Breakfast
0 件のコメント:
コメントを投稿