1

My python script reads a XML file, to give the Folder Structure.

My XML file:

<?xml version="1.0" encoding="utf-8"?>
<serverfiles name="Test">
  <serverfiles name="Fail">
    <serverfiles name="Cam1">
      <serverfiles name="Mod1">
        <serverfiles name="2019-01-07" />
        <serverfiles name="2019-01-08" />
      </serverfiles>
      <serverfiles name="Mod2">
        <serverfiles name="2019-02-07" />
        <serverfiles name="2019-02-08" />
      </serverfiles>
    </serverfiles>
  </serverfiles>
  <serverfiles name="Pass">
    <serverfiles name="Cam1">
      <serverfiles name="Mod1">
        <serverfiles name="2019-03-07" />
        <serverfiles name="2019-03-08" />
      </serverfiles>
      <serverfiles name="Mod2">
        <serverfiles name="2019-04-07" />
        <serverfiles name="2019-04-08" />
      </serverfiles>
    </serverfiles>
  </serverfiles>
</serverfiles>

Python script:

from pprint import pprint
import xml.etree.ElementTree as ET

def walk(e):
    name = e.attrib['name']
    children = [walk(c) for c in e if e.tag == 'serverfiles']
    return {'name': name, 'children': children} if children else {'name': name, 'path': ''}

file = ET.parse(r'folder_structure.xml')
r = file.getroot()
s = walk(r)
pprint(s)

This yields the following output:

{'children': [{'children': [{'children': [{'children': [{'name': '2019-01-07',
                                                         'path': ''},
                                                        {'name': '2019-01-08',
                                                         'path': ''}],
                                           'name': 'Mod1'},
                                          {'children': [{'name': '2019-02-07',
                                                         'path': ''},
                                                        {'name': '2019-02-08',
                                                         'path': ''}],
                                           'name': 'Mod2'}],
                             'name': 'Cam1'}],
               'name': 'Fail'},
              {'children': [{'children': [{'children': [{'name': '2019-03-07',
                                                         'path': ''},
                                                        {'name': '2019-03-08',
                                                         'path': ''}],
                                           'name': 'Mod1'},
                                          {'children': [{'name': '2019-04-07',
                                                         'path': ''},
                                                        {'name': '2019-04-08',
                                                         'path': ''}],
                                           'name': 'Mod2'}],
                             'name': 'Cam1'}],
               'name': 'Pass'}],  'name': 'Test'}

But my desired output is:

{'children': [{'children': [{'children': [{'children': [{'name': '2019-01-07',
                                                         'path': '/Test/Fail/Cam1/Mod1/'},
                                                        {'name': '2019-01-08',
                                                         'path': '/Test/Fail/Cam1/Mod1/'}],
                                           'name': 'Mod1'},
                                          {'children': [{'name': '2019-02-07',
                                                         'path': '/Test/Fail/Cam1/Mod2/'},
                                                        {'name': '2019-02-08',
                                                         'path': '/Test/Fail/Cam1/Mod2/'}],
                                           'name': 'Mod2'}],
                             'name': 'Cam1'}],
               'name': 'Fail'},
              {'children': [{'children': [{'children': [{'name': '2019-03-07',
                                                         'path': '/Test/Pass/Cam1/Mod1/'},
                                                        {'name': '2019-03-08',
                                                         'path': '/Test/Pass/Cam1/Mod1/'}],
                                           'name': 'Mod1'},
                                          {'children': [{'name': '2019-04-07',
                                                         'path': '/Test/Pass/Cam1/Mod2/'},
                                                        {'name': '2019-04-08',
                                                         'path': '/Test/Pass/Cam1/Mod2/'}],
                                           'name': 'Mod2'}],
                             'name': 'Cam1'}],
               'name': 'Pass'}],  'name': 'Test'}

I have referred access ElementTree node parent node and how to get xpath from root in python while parsing xml, but couldn't come up with a solution.

How do I get the path(starting from Root Node) for each of the leaf node, while parsing a XML file?

Virat
  • 129
  • 4
  • 20

1 Answers1

2

You probably have forgotten to keep the track of the running names which was required for the final path. I might be wrong for a generic use case, but the particular problem you have given, script below should work.

from pprint import pprint
import xml.etree.ElementTree as ET


def walk(e, runningname=''):
    name = e.attrib['name']

    # TODO: checking whether this is the leaf node
    # perhaps there are better ways
    if len(e) > 0:
        runningname += f'/{name}'

    children = [walk(c, runningname) for c in e if e.tag == 'serverfiles']

    return {'name': name, 'children': children} if children else {'name': name, 'path': runningname}


file = ET.parse(r'r.xml')
r = file.getroot()
s = walk(r)
pprint(s)
Virat
  • 129
  • 4
  • 20
hhsecond
  • 62
  • 7
  • Hey!! I guess this solves my problem ( don't know about the edge cases though ). Thanks a lot!! – Virat May 03 '19 at 11:47