Python 使用xml树iter方法,但要注意给定分支的结尾

Python 使用xml树iter方法,但要注意给定分支的结尾,python,xml,Python,Xml,假设我有一个包含多个child的xml文件,我希望迭代到其中一个分支的末尾 <bookstore> <classical>This classiccal books</classical> <date>2017-05-31</date> <schedule> <type>Blues</type> <region>

假设我有一个包含多个child的xml文件,我希望迭代到其中一个分支的末尾

<bookstore>
  <classical>This classiccal books</classical>
     <date>2017-05-31</date>
        <schedule>
            <type>Blues</type>
            <region>
                 <name>Alaska</name>
                 <transaction>
                     <transactionNo>455</transactionNo>
        ...
        </schedule>
        <schedule>
        </schedule>
        <anotherTag>
        </anotherTag>
...
您可以获得所需的元素,然后逐个循环,而不是遍历所有内容:

import xml.etree.ElementTree as ET
xml_data = '''
<bookstore>
    <schedule><type>Foo</type></schedule>
    <schedule><type>Bar</type></schedule>
    <schedule><type>Baz</type></schedule>
</bookstore>
'''
tree = ET.fromstring(xml_data)
for schedule in tree.findall("schedule"):
    print schedule.find("type").text
将xml.etree.ElementTree作为ET导入
xml_数据=“”
福
酒吧
巴兹
'''
tree=ET.fromstring(xml\u数据)
对于树中的计划。findall(“计划”):
打印计划。查找(“类型”)。文本
您可以获取所需的元素,然后逐个循环,而不是遍历所有内容:

import xml.etree.ElementTree as ET
xml_data = '''
<bookstore>
    <schedule><type>Foo</type></schedule>
    <schedule><type>Bar</type></schedule>
    <schedule><type>Baz</type></schedule>
</bookstore>
'''
tree = ET.fromstring(xml_data)
for schedule in tree.findall("schedule"):
    print schedule.find("type").text
将xml.etree.ElementTree作为ET导入
xml_数据=“”
福
酒吧
巴兹
'''
tree=ET.fromstring(xml\u数据)
对于树中的计划。findall(“计划”):
打印计划。查找(“类型”)。文本

我的回答有用吗?如果是,请投票,如果它解决了你的问题,请标记接受。谢谢我的回答有用吗?如果是,请投票,如果它解决了你的问题,请标记接受。谢谢