XML findall在第一次查找时停止

问题描述 投票:0回答:1

我试图使用python 2.7和xml.etree.ElementTree解析XML。

它仅适用于树中的第一个元素,不会迭代它应捕获的所有其他标记。

下面是我用来遍历XML的循环。

prefixes=['conn','dummy']
for datasource in root.findall('.//datasource'):
        for relation in datasource.findall('.//relation'):
            if 'connection' in relation.attrib:
                if relation.attrib['connection'].startswith(tuple(prefixes)): 
                    sql = SQL(relation.text)

                    if sql.isCustom() is True:
                        return sql.findTables()
                    else:    
                        print(relation.attrib['table'])
                        return relation.attrib['table']

XML看起来像这样。

<datasources>
    <datasource>
        <connection>
            <relation connection="conn.dbe" table="table1">
    <datasource>
        <connection>
            <relation connection="conn.abc" table="table2">

我已经尝试使用iterall以及findall('.//* relation')作为匹配模式,但到目前为止还没有任何工作。

python xml
1个回答
0
投票

我只得到第一张桌子的原因是我在找到它后返回,因此从未完成循环。

© www.soinside.com 2019 - 2024. All rights reserved.