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('..')
7
  • Change your approach: 1. for receipe in xml.findall('receipe'). 2. for wire in receipe.findall('wire') Commented Nov 13, 2018 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 Commented Nov 13, 2018 at 18:34
  • Possible duplicate of 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 Commented Nov 13, 2018 at 20:22
  • Tried these posts. Nothing worked for my requirement above Commented Nov 14, 2018 at 3: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 Commented Nov 14, 2018 at 17:41

1 Answer 1

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'}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.