I have an XML and part of it looks like this:
<?xml version="1.0" encoding="UTF-8" ?>,
<Settings>,
<System>,
<Format>Percent</Format>,
<Time>12 Hour Format</Time>,
<Set>Standard</Set>,
</System>,
<System>,
<Format>Percent</Format>,
<Time>12 Hour Format</Time>,
<Set>Standard</Set>,
<Alarm>ON</Alarm>,
<Haptic>ON</Haptic>'
</System>
</Settings>
What I would like to do is use xpath to specify the path //Settings/System and get the tags and values in system so that I can populate a dataframe with the following output:
| Format | Time| Set| Alarm| Haptic|
|:_______|:____|:___|______|_______|
| Percent| 12 Hour Format| Standard| NaN| NaN|
| Percent| 12 Hour Format| Standard| ON| ON|
So far I have seen methods as follows:
import xml.etree.ElementTree as ET
root = ET.parse(filename)
result = ''
for elem in root.findall('.//child/grandchild'):
# How to make decisions based on attributes even in 2.6:
if elem.attrib.get('name') == 'foo':
result = elem.text
These methods explicitly mention elem.attrib.get('name') which I would not be able to use in my case because of inconsistent elements within my /System tag. So what I am asking is if there is a method to use xpath (or anything else) which I can specify /System and get all elements and their values?
/Systemelements with different numbers of child elements (say, 3 in the first and 5 in the second) together with the expected output dataframe.