Python XML findall在第一次查找时停止
我正在尝试使用Python2.7和XML.etree.ElementTree解析XML 它只对树中的第一个元素起作用,不会遍历它应该捕获的所有其他标记 下面是我用来遍历XML的循环Python XML findall在第一次查找时停止,python,xml,Python,Xml,我正在尝试使用Python2.7和XML.etree.ElementTree解析XML 它只对树中的第一个元素起作用,不会遍历它应该捕获的所有其他标记 下面是我用来遍历XML的循环 prefixes=['conn','dummy'] for datasource in root.findall('.//datasource'): for relation in datasource.findall('.//relation'): if 'connectio
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看起来像这样。
我曾尝试使用iterall和findall(“./*关系”)作为匹配模式,但到目前为止没有任何效果 我只获取第一个表的原因是,我在找到它后返回,因此从未完成循环。好吧,您在
sql.isCustom()测试的if
和else
上返回的是relation.attrib['connection'].startswith(tuple(prefixes))
。如果你的问题比这更严重,你应该发布一个帖子,包括实际和预期的产出。还有,接受答案的三年干旱期是怎么回事?因为答案是在我问了他们大部分问题后一年来的,我已经离开了。至于问题,我的困惑在于为什么它只适用于第一个实例,而不适用于文档中的其余部分。如果你在不到一年的时间内没有得到有用的答案,那么你真的必须改进你的问题。例如,这里您未能提供一个。你应该回到你以前的问题,接受有用的答案。如果您想要更快的答案,请通过阅读和入门,并在此处添加MCVE来改进您的问题。祝你好运
<datasources>
<datasource>
<connection>
<relation connection="conn.dbe" table="table1">
<datasource>
<connection>
<relation connection="conn.abc" table="table2">