I have the below xml file named test.xml
:
<?xml version="1.0"?>
<schematic>
<receipe id="1" name="" count="4">
<wire source="24:TbPowerSupplyChassisConnectionPoint_1"/>
<wire source="25:TbPowerSupplyChassisConnectionPoint_1"/>
<wire source="26:TbPowerSupplyChassisConnectionPoint_1"/>
</receipe>
<receipe id="2" name="" count="3">
<wire source="14:TbPowerSupplyChassisConnectionPoint_1"/>
<wire source="15:TbPowerSupplyChassisConnectionPoint_1"/>
<wire source="16:TbPowerSupplyChassisConnectionPoint_1"/>
</receipe>
</schematic>
I am parsing the file with ElementTree
in Python, and the requirement is that I should get all the "wire" elements from the whole xml file using findall()
method of ElementTree
. When I have the list of wire element, I need to get parent recipe tag for some of the wire element. Sample Python code below:
import xml.etree.ElementTree as ET
print "parse"
xml = ET.parse("test.xml")
for wire in xml.findall('.//wire'):
print wire.get('source').split(':')[0]
//Need to get parent receipe element here from wire
I search all over and got some methods, but nothing seems working fine. Also followed few Stack Overflow posts, but it didn't work either. Any help would be highly appreciated. Below are some of the methods I tried using, but none worked.
wire.iterancestors()
wire.get('parent')
wire.getroot()
wire.find('..')