13

このxml.etree.ElementTreeモジュールを使用して、別の構造化ドキュメントから Python 3.1 で XML ドキュメントを作成しています。

既存のサブ要素のインデックスを返すには、どのElementTree関数を使用できますか?

4

3 に答える 3

12

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
于 2010-09-21T18:00:57.707 に答える
-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
于 2017-01-05T08:46:25.350 に答える