This is driving me mental, and I've probably been hacking away at it for to long so would appreciate some help to prevent lose of/restore my sanity! The food based xml is only an example of what I wish to achieve.
I have the following file which I am trying to put into a graph, so wheat and fruit are parents with a depth of 0. Indian is a child of wheat with a depth of 1 and so on and so on.
Each of the layers has some keywords. So what I want to get out is
layer, depth, parent, keywords
wheat, 1, ROOT, [bread, pita, narn, loaf]
indian, 2, wheat [chapati]
mumbai, 3, indian, puri
fruit, 1,ROOT, [apple, orange, pear, lemon]
This is a sample file -
<keywords>
<layer id="wheat">
<layer id="indian">
<keyword>chapati</keyword>
<layer id="mumbai">
<keyword>puri</keyword>
</layer>
</layer>
<keyword>bread</keyword>
<keyword>pita</keyword>
<keyword>narn</keyword>
<keyword>loaf</keyword>
</layer>
<layer id="fruit">
<keyword>apple</keyword>
<keyword>orange</keyword>
<keyword>pear</keyword>
<keyword>lemon</keyword>
</layer>
</keywords>
So this isnt a graph question, I can do that bit thats easy. What im struggling with is parsing the XML.
If I do a
xmldoc = minidom.parse(self.filename)
layers = xmldoc.getElementsByTagName('layer')
layers only returns all of the layer elements, which is to much and has not concept of depth/ hierachy as far as I can understand as it does a recursive search.
The following post is good, but doesnt provide the concepts I require. XML Parsing with Python and minidom. Can anyone help with how I might go about this? I can post my code but its so hacked together/fundementally broken I don't think it would be use to man nor beast!
Cheers
Dave