我有一个xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<ItemList>
<Item iID="12345" iName="absbd">
<IList>
<I iID="895741" iName="anbgtet" iExID="12345" Qty="1.000000">
<BcList>
<Bcode bID="788458" bc="122346" />
</BcList>
</I>
</IList>
</Item>
<Item iID="45679" iName="absasdsadbd">
<IList>
<I iID="8589" iName="adsad" iExID="5345" Qty="1.000000">
<BcList>
<Bcode bID="45477" bc="457859" />
<Bcode bID="114525" bc="445785" />
</BcList>
</I>
</IList>
</Item>
</ItemList>
我只需要检索那些具有多个Bcode标签的BcList标签
我正在使用python,到目前为止,我只能获得单独的Bcode值。任何人都可以帮助我
import xml.etree.ElementTree as ET
import re
xmlfilePath = 'path of xml file'
tree = ET.parse(xmlfilePath)
root = tree.getroot()
for elem in root.iter(tag='Bcode'):
#print(elem.tag, elem.attrib)
print(elem.attrib['bc'])
#print(elem.attrib)
答案 0 :(得分:0)
尝试获取tag='BcList'
的长度然后处理
<强>实施例强>
tree = ET.parse(xmlfilePath)
root = tree.getroot()
for elem in root.iter(tag='BcList'):
if len(elem) > 1:
for Bcode in elem.iter(tag='Bcode'):
print( Bcode.attrib['bc'] )
<强>输出:强>
457859
445785