2012-10-01 49 views
0

我的XML文件中獲取總數在root.iter(),有根的10分兒童(同一層次),其名稱爲「測試用例」如何ElementTree的

我不能圖出以下幾點:首先 ,我做下面讓所有的子兒:

for testCase in root.iter('testCase'): 
  1. 我需要從最後一個子兒的一些屬性「測試用例」。但是我怎麼知道它是最後一個'testCase'。有沒有辦法數它們?
  2. 另外,有沒有辦法訪問第n個子孩子而不必經過iter()?

回答

2

關於這種類型的操作,你應該使用XPath,這是瀏覽XML樹的常用簡單方法。我不認爲標準的Python的ElementTree支持的XPath,但lxml不(很常用的爲好),這裏有一個例子:

獲取最後一個子項:

>>> text = """<Root> 
    <Child name="child1" /> 
    <Child name="child2" /> 
    <Child name="child3" /> 
    <Child name="child4" /> 
    <Child name="child5" /> 
</Root>""" 
>>> from lxml import etree 
>>> root = etree.fromstring(text) 
>>> last_tag = root.xpath('/Root/Child[last()]')[0] 
>>> last_tag.attrib['name'] 
'child5' 

直接訪問元素編號#N :

>>> tag3 = root.xpath('/Root/Child[3]')[0] 
>>> tag3.attrib['name'] 
'child3' 
3

請嘗試以下示例。請參閱下面的輸出。它顯示了什麼被用作my.xml的內容。該元素表現爲一個兒童列表(即它也可以迭代)。有函數和迭代器以獨立的方式在文檔順序中獲取所有需要的元素(即它們的深度和子元素等等無關緊要)。 element.attrib表現爲屬性字典。該標準還xml.etree.ElementTree支持XPath的子集 - 看到結尾:

import xml.etree.ElementTree as et 

tree = et.parse('my.xml') 
root = tree.getroot()  # the root element of the tree 

et.dump(root)    # here is how the input file looks inside 

# Any element behaves as a list of children. This way, the last child 
# of the list can be accessed via negative index. 
print '-------------------------------------------' 
print root[-1] 

# Here is the content. 
print '-------------------------------------------' 
et.dump(root[-1]) 

# If the elements could be not direct children, you can use findall('tag') to 
# get the list of the elements. Then you access it again as the last element 
# of the list 
print '-------------------------------------------' 
lst = root.findall('testCase') 
et.dump(lst[-1]) 

# The number of the 'testCase' elements is simply the length of the list. 
print '-------------------------------------------' 
print 'Num. of test cases:', len(lst) 

# The elem.iter('tag') works similarly. But if you want the last element, 
# you must know when the element is the last one. It means you have to 
# loop through all of them anyway. 
print '-------------------------------------------' 
last = None # init 
for e in root.iter('testCase'): 
    last = e 

et.dump(last) 

# The attributes of the elements take the form of the dictinary .attrib. 
print '-------------------------------------------' 
print last.attrib 
print last.attrib['name'] 

# The standard xml.etree.ElementTree supports a subset of XPath. You can use 
# it if you are familiar with XPath. 
print '-------------------------------------------' 
third = root.find('.//testCase[3]') 
et.dump(third) 

# ... including the last() function. For more complex cases, use lxml 
# as pointed out by Emmanuel. 
print '-------------------------------------------' 
last = root.find('.//testCase[last()]') 
et.dump(last) 

它打印出我的控制檯上執行以下操作:

c:\tmp\___python\Sunny\so12669404>python a.py 
<root> 
    <testCase name="a" /> 
    <testCase name="b" /> 
    <testCase name="c" /> 
    <testCase name="d" /> 
</root> 
------------------------------------------- 
<Element 'testCase' at 0x231a630> 
------------------------------------------- 
<testCase name="d" /> 
------------------------------------------- 
<testCase name="d" /> 
------------------------------------------- 
Num. of test cases: 4 
------------------------------------------- 
<testCase name="d" /> 
------------------------------------------- 
{'name': 'd'} 
d 
------------------------------------------- 
<testCase name="c" /> 

------------------------------------------- 
<testCase name="d" />