2014-12-09 37 views
0

我试图迭代“变体”的所有“值”标记,代码不会跳转到下一个“值”键,因为xml具有另一个“值”键“第一个值KEY”使用lxml模块分析xml文件时出现问题

<variant> 
    <name>PROGRAMS</name> 
    <value> <!-- Lets call it FIRST VALUE KEY --> 
    <value>PROG1</value> 
    <statistics> 
     <statistic name="Stats"> 
      <value>5</value> 
     </statistic> 
    </statistics> 
    </value> 
    <value> <!-- SECOND VALUE KEY --> 
    <value>PROG2</value> 
    ... 
    </value> 
</variant> 
<variant> 
    <name>OTHER</name> 
    ... 
</variant> 

这里是我的Python代码

for keys in root.iter('variant'): 
    for variant in keys: 
     if variant.text == 'PROGRAMS': 
      for value_tag in keys.iter('value'): 
       ParamValue = value_tag.find('value').text 
        if ParamValue == 'PROG2': 
         print "GOT IT!" 
        else: continue # <- this jumps to the "<value>PROG1</value>" tag 
            # but it should jump to the "SECOND VALUE KEY" 

在哪里的问题?

+0

什么是期望的输出? – unutbu 2014-12-09 13:35:05

+0

可以说它必须在'if ifParamValue =='PROG2'之后打印一些东西:'condition – Pythonizer 2014-12-09 13:36:03

回答

1
import lxml.etree as ET 
root = ET.parse('data').getroot() 

for value in root.xpath(
    '''//variant 
      [name 
      [text()="PROGRAMS"]] 
     /value 
      [value 
      [text()="PROG2"]]'''): 
    print('GOT IT') 

产生

GOT IT 

我认为这是比较容易use XPath向下挖掘到你想要的元素。 中的XPath意味着

//       # look for all elements 
variant     # that are variants 
    [name     # that have a <name> element 
    [text()="PROGRAMS"]] # with text equal to "PROGRAMS" 
/value     # select the <value> (child of variant) 
    [value     # that has a child <value> element 
    [text()="PROG2"]]  # with text equal to "PROG2" 

遍历<statistics>儿童<value>元素:

for statistics in root.xpath(
    '''//variant 
      [name 
      [text()="PROGRAMS"]] 
     /value 
      [value 
      [text()="PROG2"]] 
      /statistics'''): 

在XPath,括号[..]松散翻译成 “这样”。请注意,如果没有括号,上面的XPath将是//variant/value/statistics。它看起来有点像文件路径。和文件路径一样,它显示了元素的谱系。一个/意味着“直接子女”,而//意味着“子孙后代”(例如儿童,孙子或孙子女等)。

+0

这样一种到达目的地的简单方法:o但是还有一个问题,如何开始迭代PROG2下的“统计”键 – Pythonizer 2014-12-09 14:21:47