Python 使用 xml.etree.ElementTree 获取子节点的所有实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29266605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:17:42  来源:igfitidea点击:

Getting all instances of child node using xml.etree.ElementTree

pythonxmlxml-parsingelementtree

提问by Ash

I have the following XML file as input:

我有以下 XML 文件作为输入:

<Test>
  <callEvents>
    <moc>
      <causeForTermination>0</causeForTermination>
      <serviceCode>
        <teleServiceCode>11</teleServiceCode>
      </serviceCode>
      <dialledDigits>5555555</dialledDigits>
      <connectedNumber>77777</connectedNumber>
    </moc>

    <moc>
      <causeForTermination>0</causeForTermination>
      <serviceCode>
        <teleServiceCode>11</teleServiceCode>
      </serviceCode>
      <dialledDigits>2222222</dialledDigits>
    </moc>
  </callEvents>
  <callEventsCount>100</callEventsCount>
</Test> 

I want to output all the values for dialledDigits. However, my code only displays the first instance of dialledDigits.

我想输出 dialledDigits 的所有值。但是,我的代码只显示了 dialledDigits 的第一个实例。

dialledDigits {} 5555555

My desired output should contain both instances.

我想要的输出应该包含这两个实例。

dialledDigits {} 5555555
dialledDigits {} 2222222

Here is my code

这是我的代码

import xml.etree.ElementTree as ET
tree = ET.parse('as.xml')
root = tree.getroot()
callevent=root.find('callEvents')

Moc1=callevent.find('moc')

for node in Moc1.getiterator():
    if node.tag=='dialledDigits':
        print node.tag, node.attrib, node.text

采纳答案by Celeo

Use findall:

使用findall

moc1 = callevent.findall('moc')

for moc in moc1:
    for node in moc.getiterator():
        if node.tag=='dialledDigits':
            print node.tag, node.attrib, node.text

Output:

输出:

dialledDigits {} 5555555
dialledDigits {} 2222222

回答by Vivek Sable

find()will return first tag object, so use finadall()which return all tag objects`

find()将返回第一个标签对象,所以使用finadall()它返回所有标签对象`

>>> Moc1=callevent.find('moc')
>>> Moc1
<Element 'moc' at 0x869a2ac>
>>> Moc1=callevent.findall('moc')
>>> Moc1
[<Element 'moc' at 0x869a2ac>, <Element 'moc' at 0x869a4ec>]
>>> 

Iterate on it:

迭代它:

>>> Mocs=callevent.findall('moc')
>>> for moc in Mocs:
...     for node in moc.getiterator():
...         if node.tag=='dialledDigits':
...             print node.tag, node.attrib, node.text
... 
dialledDigits {} 5555555
dialledDigits {} 2222222

回答by alecxe

You can also write an XPath expression. Just 2 lines instead of 5 and a single loop:

您还可以编写XPath 表达式。只有 2 行而不是 5 行和一个循环:

for node in tree.findall('.//callEvents/moc/dialledDigits'):
    print node.tag, node.attrib, node.text 

Demo:

演示:

>>> import xml.etree.ElementTree as ET
>>> 
>>> 
>>> tree = ET.parse('as.xml')
>>> root = tree.getroot()
>>> 
>>> for node in tree.findall('.//callEvents/moc/dialledDigits'):
...     print node.tag, node.attrib, node.text
... 
dialledDigits {} 5555555
dialledDigits {} 2222222