Fetching the path( from root node ) for all the leaf nodes?

亡梦爱人 提交于 2019-12-13 03:43:04

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/55968311/fetching-the-path-from-root-node-for-all-the-leaf-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!