0

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('..')
Joel
  • 1,564
  • 7
  • 12
  • 20
Deca
  • 1,155
  • 1
  • 10
  • 19
  • Change your approach: **1.** `for receipe in xml.findall('receipe')`. **2.** `for wire in receipe.findall('wire')` – stovfl Nov 13 '18 at 18:25
  • Ya, that's one approach which I would have definitely used to solve this problem. It clicked me too, and looping it two times receips and then wire. But unfortunately, there is huge project already written and this cannot be changes. I am working from something in middle of code changes. There should be some way to get the parent node, and I would like to see if I get some answer – Deca Nov 13 '18 at 18:34
  • Possible duplicate of [In Python ElementTree how can I get list of all ancestors of an element in tree?](https://stackoverflow.com/questions/3041258/in-python-elementtree-how-can-i-get-list-of-all-ancestors-of-an-element-in-tree) and [Finding parent from child in XML using python](https://stackoverflow.com/questions/31617621/finding-parent-from-child-in-xml-using-python/31647398#31647398) – stovfl Nov 13 '18 at 20:22
  • Tried these posts. Nothing worked for my requirement above – Deca Nov 14 '18 at 03:31
  • @stovfl It's already there what I have tried. Moreover, the above part of code above is working code. I need the solution for the line commented. If anyone has an answer, they are free to use the code and give the solution. Should come to know by themselves if the solution provided by the person is working or the link shared by them has an answer – Deca Nov 14 '18 at 17:41
  • [Edit] your Question and show what you have tried, **from the links**, and what is not working – stovfl Nov 14 '18 at 17:47
  • It's there above. Please have a look both at the links and the things I tried. – Deca Nov 14 '18 at 18:43

1 Answers1

0

I don't know will this help ?

In [103]: from myparser import parse

In [104]:

In [104]: payload = """<?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>"""

In [105]: result = parse(payload)

In [108]: result.attr_mapping
Out[108]:
{'schematic.receipe.0.wire.0': {u'source': u'24:TbPowerSupplyChassisConnectionPoint_1'},
 'schematic.receipe.0.wire.1': {u'source': u'25:TbPowerSupplyChassisConnectionPoint_1'},
 'schematic.receipe.0.wire.2': {u'source': u'26:TbPowerSupplyChassisConnectionPoint_1'},
 'schematic.receipe.1.wire.0': {u'source': u'14:TbPowerSupplyChassisConnectionPoint_1'},
 'schematic.receipe.1.wire.1': {u'source': u'15:TbPowerSupplyChassisConnectionPoint_1'},
 'schematic.receipe.1.wire.2': {u'source': u'16:TbPowerSupplyChassisConnectionPoint_1'}}
panda912
  • 196
  • 1
  • 6