1
votes

My xml looks like below.

<RootTag xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Branch>
<BranchID>100</BranchID>
   <Child>
        <policy1>hello</policy1>
        <policy2>how are you</policy2>
   </Child>
   <Child>
        <policy1>hello1</policy1>
        <policy2>how are you1</policy2>
   </Child>
</Branch>
<Branch>
<BranchID>200</BranchID>
<Child>
        <policy1>I am good</policy1>
        <policy2>how about you</policy2>
</Child>
<Child>
        <policy1>I am good1</policy1>
        <policy2>how about you1</policy2>
</Child>
</Branch>
</RootTag>

I tried using getiterator(beacuse I'm in python 2.6) to get all the elements. I am able to flatten them but the parent tag(BranchID) values should be present in all the rows of its child.

Output expected:

BranchID,policy1,policy2
100,hello,how are you
100,hello1,how are you1
200,I am good,how about you
200,I am good1,how about you1
1
should both BranchID element really be siblings under the same Branch element? or each should have its own parent Branch element? - Adam.Er8
They have their own parent branch. I have updated the xml, thanks for pointing it out. - Mohanram Krishnan
OK, Great, I'm posting a solution, let me now if it worked :) - Adam.Er8

1 Answers

0
votes

please try this code (remember to replace YOUR FILE HERE with an actual file path):

import xml.etree.ElementTree as ET

root = ET.parse('YOUR FILE HERE')  # replace file name

print("BranchID,policy1,policy2")
branches = root.findall('.//Branch')
for branch in branches:
    branch_id = branch.find("BranchID").text
    for child in branch.findall('.//Child'):
        policy1 = child.find('policy1').text
        policy2 = child.find('policy2').text
        print("{},{},{}".format(branch_id,policy1,policy2))