【问题标题】:How to iterate through child element using child element如何使用子元素遍历子元素
【发布时间】:2019-07-04 17:22:08
【问题描述】:

我需要在默认字典中输入 XML 文件的元素。 使用第一个子元素作为我的键,其他 2 个值将在列表中。

我尝试将元素保留在单独的列表中,然后将它们配对,但仍然没有成功。

下面是 XML 结构

<lines>
    <line>
        <lineName>'Line 1'</lineName>
        <lineCode>'5501'</lineCode>
        <machineName>'Line_1'</machineName>
    </line>
    <line>
        <lineName>'Line 2'</lineName>
        <lineCode>'5502'</lineCode>
        <machineName>'Line_2'</machineName>
    </line>
</lines>

这是我检索元素的方式

item = myxmlcfg.getElementsByTagName('lineName')

item 是一个包含 2 个元素的列表

item['Line 1', 'Line 2']

lineCode 和 machineName 元素也是如此

所以我需要一个输出如下的默认字典

lines {'Line 1': ['5501', 'Line_1'], 'Line 2':['5502', 'Line_2']}

其中KeylineName 标签,value 是一个包含两个元素的列表,其值为lineCodemachineName

您能建议我一种方法来迭代 xml 元素以获得上述输出吗? 任何帮助将不胜感激。 谢谢

【问题讨论】:

    标签: python xml python-3.7 minidom


    【解决方案1】:

    我没有使用minidom 的任何经验,但使用ElementTree 是一项微不足道的任务:

    from xml.etree import ElementTree as ET
    from xml.etree.ElementTree import ElementTree
    
    if __name__ == '__main__':
    
        xml_raw = '''
        <lines>
            <line>
                <lineName>'Line 1'</lineName>
                <lineCode>'5501'</lineCode>
                <machineName>'Line_1'</machineName>
            </line>
            <line>
                <lineName>'Line 2'</lineName>
                <lineCode>'5502'</lineCode>
                <machineName>'Line_2'</machineName>
            </line>
        </lines>
        '''
    
        root: ElementTree = ET.fromstring(xml_raw)
        lines = {}
        for line in root.findall('line'):
            name = line.findtext('lineName').strip("'")
            code = line.findtext('lineCode').strip("'")
            machine = line.findtext('machineName').strip("'")
            lines[name] = [code, machine]
        print(lines)
    

    输出:

    {'Line 1': ['5501', 'Line_1'], 'Line 2': ['5502', 'Line_2']}
    

    【讨论】:

    • 最后一个问题,因为我开始寻找 ElementTree。要从root获取特定元素,例如我的root是Configuration,在root里面,我应该寻找的元素是标签,我如何在这样的元素中迭代?
    【解决方案2】:

    使用lxml.etreeElement.itersiblings()

    from lxml import etree
    
    root=etree.fromstring(xml)
    item=root.findall(f'.//lineName')
    lines={i.text.strip("'"): [s.text.strip("'") for s in i.itersiblings()] for i in item}
    

    输出:

    {'Line 1': ['5501', 'Line_1'], 'Line 2': ['5502', 'Line_2']}
    

    【讨论】:

      【解决方案3】:

      这里

      import xml.etree.ElementTree as ET
      
      xml = '''<lines>
          <line>
              <lineName>'Line 1'</lineName>
              <lineCode>'5501'</lineCode>
              <machineName>'Line_1'</machineName>
          </line>
          <line>
              <lineName>'Line 2'</lineName>
              <lineCode>'5502'</lineCode>
              <machineName>'Line_2'</machineName>
          </line>
      </lines>'''
      
      root = ET.fromstring(xml)
      lines = root.findall('.//line')
      
      
        data = {
      line.find('lineName').text.strip("'"): [line.find('lineCode').text.strip("'"), line.find('machineName').text.strip("'")]
      for line in lines}
      

      打印(数据)

      输出

      {'Line 1': ['5501', 'Line_1'], 'Line 2': ['5502', 'Line_2']}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-02
        • 2016-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-23
        相关资源
        最近更新 更多