test02.py
# -*- coding:utf-8 -*- import xml.dom.minidom # Node のデータを表示する def dispNodeData(node, tag): l = node.getElementsByTagName(tag) for n in l: print n.nodeName, " - ", n.childNodes.item(0).nodeValue # Main function dom = xml.dom.minidom.parse('test01.xml') foods = dom.getElementsByTagName('food') for food in foods: print "nodeName: ", food.nodeName dispNodeData(food, 'name') dispNodeData(food, 'price') dispNodeData(food, 'calories')実行結果
> python test02.py nodeName: food name - Belgian Waffles price - $5.95 calories - 650 nodeName: food name - Strawberry Belgian Waffles price - $7.95 calories - 900 nodeName: food name - Berry-Berry Belgian Waffles price - $8.95 calories - 900 nodeName: food name - French Toast price - $4.50 calories - 600 nodeName: food name - Homestyle Breakfast price - $6.95 calories - 950
0 件のコメント:
コメントを投稿