このxml.etree.ElementTree
モジュールを使用して、別の構造化ドキュメントから Python 3.1 で XML ドキュメントを作成しています。
既存のサブ要素のインデックスを返すには、どのElementTree
関数を使用できますか?
このxml.etree.ElementTree
モジュールを使用して、別の構造化ドキュメントから Python 3.1 で XML ドキュメントを作成しています。
既存のサブ要素のインデックスを返すには、どのElementTree
関数を使用できますか?
getchildrenメソッドは、Elementオブジェクトのサブ要素のリストを返します。次に、リストの組み込みのインデックスメソッドを使用できます。
>>> import xml.etree.ElementTree as ET
>>> root = ET.Element("html")
>>> head = ET.SubElement(root, "head")
>>> body = ET.SubElement(root, "body")
>>> root.getchildren().index(body)
1
def alarms_validation(self, path, alarm_no, alarm_text):
with open(path) as f:
tree = et.parse(f)
root = tree.getroot()
try:
for x in xrange(10000):
print x
for y in xrange(6):
print y
if root[x][y].text == alarm_no:
print "found"
if root[x][y+1].text != alarm_text:
print "Alarm text is not proper"
else:
print "Alarm Text is proper"
except IndexError:
pass