A noob question on XML. I am using ElementTree to convert the XML to a df and eventually upload to a database. My problem is that the XML format is not standard. All ElementTree examples that I have seen use a different format. It looks something like this:
<session session_id="9">
<party party_id="1" name="party 1">
<member member_id="1" name="bob"/>
<member member_id="2" name="simon"/>
</party>
<party party_id="2" name="party 2">
<member member_id="3" name="diana"/>
<member member_id="4" name="pablo"/>
</party>
</session>
For one thing, all information is written in attributes, which is not a big issue because I can still fetch them. However I want to pick up the attributes not only of the member
elements, but also of their parents. So the result should look something like this:
member_id | member_name | party_id | session_id |
---|---|---|---|
1 | bob | 1 | 9 |
2 | simon | 1 | 9 |
3 | diana | 2 | 9 |
4 | pablo | 2 | 9 |
I use children = list(root.iter())
to list all children and then append their attributes to a dataframe. However I lose the link to parent, so I cannot really say which party
branch the member
came from, so I cannot assign the right party_id
.
I am wondering whether there is an easy way to get a dataframe out of this XML structure?