Create a list of attribute of nested nodes from XML file

Viewed 629

I have an XML file as following:

<?xml version="1.0" encoding="utf-8"?>
<files>
    <file name="1">
        <file name="4">     
        </file> 
    </file>
    <file name="2">
    </file>
    <file name="3">
        <file name="5">
            <file name="7">
            </file>
        </file>
    </file>
</files>

Now I want to create a list of strings/numbers which save all the attribute name in a list/array which has the nested nodes hierarchy in it. For example for the above XML file the expected list would be,

(1,4
2
3,5,7)

Because I could could know the desired node in which level is.

Would you please let me know what is your idea about having such a list?

Update: After Jon's answer, If the children nodes are in the same hierarchy, then would in the following way.

The XML file:

<files>
<file name="1">
    <file name="4"/>     
    <file name="2">
      <file name="3"/>
    </file> 
</file>
<file name="5"/>

and the desired output:

1,4
1,2
1,2,3
5

PS. After testing for some examples I noticed that I have to have first parent and then children in case when there are two or more children from the same level.

2 Answers
Related