Help, I have the following XML file that I am trying to read and extract data from, below is an extract from the xml file,
<Variable name="Inboard_ED_mm" state="Output" type="double[]">17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154<Properties><Property name="index">25</Property><Property name="description"></Property><Property name="upperBound">0</Property><Property name="hasUpperBound">false</Property><Property name="lowerBound">0</Property><Property name="hasLowerBound">false</Property><Property name="units"></Property><Property name="enumeratedValues"></Property><Property name="enumeratedAliases"></Property><Property name="validity">true</Property><Property name="autoSize">true</Property><Property name="userSlices"></Property></Properties></Variable>
I am trying to extract the following, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154
I have worked through the example here, xml.etree.ElementTree — The ElementTree XML API and I can get the example to work, but when I modify the code for the above xml, the code returns nothing!
Here is my code,
import xml.etree.ElementTree as ET
work_dir = r"C:\Temp\APROCONE\Python"
with open(model.xml, 'rt') as f:
tree = ET.parse(f)
root = tree.getroot()
for Variable in root.findall('Variable'):
type = Variable.find('type').text
name = Variable.get('name')
print(name, type)
Any ideas? Thanks in advance for any help.
Edit: Thanks to everyone who has commented. With with your advice I have had a play and a search and got the following code,
with open(os.path.join(work_dir, "output.txt"), "w") as f:
for child1Tag in root.getchildren():
for child2Tag in child1Tag.getchildren():
for child3Tag in child2Tag.getchildren():
for child4Tag in child3Tag.getchildren():
for child5Tag in child4Tag.getchildren():
name = child5Tag.get('name')
if name == "Inboard_ED_mm":
print(child5Tag.attrib, file=f)
print(name, file=f)
print(child5Tag.text, file=f)
To return the following,
{'name': 'Inboard_ED_mm', 'state': 'Output', 'type': 'double[]'}
Inboard_ED_mm
17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154
I know, not the best code in the world, any ideas on how to streamline the code would very welcome.
Variableis in a default namespace. Do you have axmlns="???"anywhere in the XML that's not shown?