Warm tip: This article is reproduced from stackoverflow.com, please click
python tree xml xml-parsing

Parsing the root node, to fetch the entire file structure?

发布于 2020-03-27 10:17:34

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

My XML file:

<?xml version="1.0" encoding="utf-8"?>
<folderstructure>
  <folder name="Fail">
    <folder name="Cam 1">
      <folder name="Mod1">
        <folder name="2019-04-09" />
      </folder>
    </folder>
    <folder name="Cam 2">
      <folder name="Mod1">
        <folder name="2019-04-09" />
      </folder>
    </folder>
  </folder>
  <folder name="Pass">
    <folder name="Cam 1">
      <folder name="Mod1">
        <folder name="2019-04-09" />
      </folder>
    </folder>
    <folder name="Cam 2">
      <folder name="Mod1">
        <folder name="2019-04-09" />
      </folder>
    </folder>
  </folder>
</folderstructure>

I wrote the following script, with reference to Fetching the path( from root node ) for all the leaf nodes (My previous Question):

def walk(e, runningPath='', flag = 1):
    name = e.attrib['name']

    if len(e)>0:
            runningPath += '/' + name
    children = [walk(c, runningPath, 0) for c in e if ((e.tag == 'folderstructure' and flag==1) or (e.tag=='folder' and flag == 0))]
    print(children)
    return {'name': name, 'children': children} if children else {'name': name, 'path': runningPath + '/' + name}

But the above script yields 'None' as the output.

My desired output is:

{'children': [{'children': [{'children': [{'children': [{'name': '2019-04-09',
                                                         'path': '/Fail/Cam '
                                                                 '1/Mod1/2019-04-09'}],
                                           'name': 'Mod1'}],
                             'name': 'Cam 1'},
                            {'children': [{'children': [{'name': '2019-04-09',
                                                         'path': '/Fail/Cam '
                                                                 '2/Mod1/2019-04-09'}],
                                           'name': 'Mod1'}],
                             'name': 'Cam 2'}],
               'name': 'Fail'},
              {'children': [{'children': [{'children': [{'name': '2019-04-09',
                                                         'path': '/Pass/Cam '
                                                                 '1/Mod1/2019-04-09'}],
                                           'name': 'Mod1'}],
                             'name': 'Cam 1'},
                            {'children': [{'children': [{'name': '2019-04-09',
                                                         'path': '/Pass/Cam '
                                                                 '2/Mod1/2019-04-09'}],
                                           'name': 'Mod1'}],
                             'name': 'Cam 2'}],
               'name': 'Pass'}]
}

How do I go about solving this ?

Questioner
Virat
Viewed
67
Ilya Miroshnichenko 2019-07-03 21:31

Your function returns None in case of some exception. Using block try: except you are catching any exception, so you can't face the reason of the problem, try to remove this block from code to see the problem, or catch more specific exception. And as I see 'folderstructure' has no name, you can fix this by adding <folderstructure name='some name'> in your xml or set default name for your root element. code below seems working:

def walk(e, runningPath='', flag = 1):
        try:
            name = e.attrib['name']
        except KeyError:
            name = 'root'

        if len(e)>0:
            runningPath += '/' + name
        children = [walk(c, runningPath, 0) for c in e if ((e.tag == 'folderstructure' and flag==1) or (e.tag=='folder' and flag == 0))]
        print(children)
        return {'name': name, 'children': children} if children else {'name': name, 'path': runningPath + '/' + name}