2013-02-14 118 views
0

我想獲取特定組件的一些值。例如,我想從下面的輸出中只提取2個值(即Component - > name:paristrain和Stat - > TimeoutValue:value)。我試圖用xpath來做到這一點,但我無法獲得所需的輸出。你能幫我解決這個問題嗎?用元素樹獲取值

from xml.etree import ElementTree 

with open('rejexstats.xml', 'rt') as f: 
    tree = ElementTree.parse(f) 

for node in tree.iter(): 
    print node.tag, node.attrib 

此打印:

Statistics {} 
{http://www.rejex.com/stats}Server {'start': '2013-01-22T22:30:13.583', 'product': 'rejex', 'end': '2013-01-23T09:39:45.249', 'startup': '2013-01-22T22:30:13.583', 'name': 'localhost'} 
{http://www.rejex.com/statistics}Component {'subtype': 'Thread', 'type': 'Supplier', 'name': 'paristrain'} 
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'TimeoutValue', 'value': '120'} 
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'PendingRequests', 'value': '0'} 
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'Session|0|SupplierTimeout', 'value': '0'} 
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'Session|0|Errors', 'value': '0'} 
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'Session|3|SupplierTimeout', 'value': '0'} 
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'ApplyRulesErrors', 'value': '0'} 

XML文件

<Statistics> 
    <Server end="2013-02-14T07:06:35.533" name="localhost" product="regex" start="2013-02-13T22:30:12.982" startup="2013-02-13T22:30:12.982"> 
     <Component name="paristrain" subtype="Thread" type="Supplier"> 
      <Stat name="TimeoutValue" type="entry" value="120"/> 
      <Stat name="PendingRequests" type="entry" value="0"/> 
      <Stat name="Session|0|SupplierTimeout" type="entry" value="0"/> 
      <Stat name="Session|0|Errors" type="entry" value="0"/> 
      <Stat name="Session|3|SupplierTimeout" type="entry" value="0"/> 
      <Stat name="ApplyRulesErrors" type="entry" value="0"/> 
      <Stat name="LateResponses" type="entry" value="0"/> 
      <Stat name="CacheTries" type="entry" value="0"/> 
      <Stat name="Session|4|Errors" type="entry" value="0"/> 
      <Stat name="MaxActiveThreads" type="entry" value="0"/> 
      <Stat name="MaxPendingQueueSize" type="entry" value="10"/> 
      <Stat name="ValidResponses" type="entry" value="0"/> 
      <Stat name="TranslateResponses" type="entry" value="0"/> 

回答

0

您需要在您的XPath查詢完整的命名空間:

for component in tree.iterfind('{http://www.rejex.com/statistics}Component'): 
    print component.attrib['name'] 

或者,你可以使用一個明確的命名空間映射,一個是ma PS前綴(選擇)來命名空間URI:

nsmap = {'rej': 'http://www.rejex.com/statistics`} 

for stat in tree.iterfind('rej:Stat', namespaces=nsmap): 
    print stat.attrib['value'] 

rej前綴無論你傳遞爲namespaces,然後轉化到在第一個例子給出了相同的XPath查詢擡頭。

您可以在{namespace}的XPath預選賽展開尋找更復雜的比賽:

tree.find(
    "{http://www.rejex.com/statistics}Component[@name='paristrain']/" 
    "{http://www.rejex.com/statistics}Stat[@name='TimeoutValue']") 

應該返回具有屬性name="TimeoutValue"其父是Component元素與name="paristrain"屬性,例如Stat元素。

+0

事情是在我的XML文件中有這麼多的組件。我只想獲取組件paristrain的值和它的TimeoutValue:Value。 – 2013-02-14 10:20:03

+0

@fear_matrix:您可以使用名稱空間前綴爲其創建XPath表達式。 – 2013-02-14 10:20:40

+0

@fear_matrix:添加了未經測試的示例XPath表達式。您沒有包含任何示例XML,因此很難爲您測試。 – 2013-02-14 10:29:00