I have an XML file in the following format:
<doc>
<id name="X">
<type name="A">
<min val="100" id="80"/>
<max val="200" id="90"/>
</type>
<type name="B">
<min val="100" id="20"/>
<max val="20" id="90"/>
</type>
</id>
<type...>
</type>
</doc>
I would like to parse this document and build a hash table
{X: {"A": [(100,80), (200,90)], "B": [(100,20), (20,90)]}, Y: .....}
How would I do this in Python?
As others have stated minidom is the way to go here. You open (and parse) the file, while going through the nodes you check if its relevant and should be read. That way, you also know if you want to read the child nodes.
Threw together this, seems to do what you want. Some of the values are read by attribute position rather than attribute name. And theres no error handling. And the print () at the end means its Python 3.x.
I’ll leave it as an exercise to improve upon that, just wanted to post a snippet to get you started.
Happy hacking! 🙂
xml.txt
parsexml.py
Output: