簡體   English   中英

ElementTree元素索引查找

[英]ElementTree element index look up

我正在使用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
import xml.etree.ElementTree as ET
root=ET.Element('C:\Users\Administrator\Desktop\ValidationToolKit_15.9\ValidationToolKit_15.9\NE3S_VTK\webservice\history\ofas.2017-1-3.10-55-21-608.xml')
childnew=ET.SubElement(root,"354")
root.getchildren().index(childnew)
0
list(root).index(childnew)
0
def Alarms_Validation(self,path,AlarmNo,AlarmText,Severity,Text,Event):
    with open(path) as f:
        tree = et.parse(f)
        RUN=True
        root = tree.getroot()
        try:

                for x in xrange(10000):
                        print x
                        for y in xrange(6):
                                print y
                                if root[x][y].text==AlarmNo:
                                        print "found"
                                        print x
                                        print y
                                        if root[x][y+1].text!=AlarmText:
                                            print "Alarm text is not proper"
                                        else:
                                            print "Alarm Text is proper"
        except IndexError:
                pass

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM