title: Parse Nested XML using python
body: <p>I'm trying to parse Nested XML using python. Sample file format looks like this</p>
  
  <pre><code>&lt;repositoryFileTreeDto&gt;
      &lt;children&gt;
          &lt;children&gt;
              &lt;file&gt;
                  &lt;name&gt; File1 &lt;/name&gt;
                  &lt;path&gt; home/user1/File1.txt &lt;/path&gt;
              &lt;/file&gt;
          &lt;/children&gt;
          &lt;children&gt;
              &lt;file&gt;
                  &lt;name&gt; File2 &lt;/name&gt;
                  &lt;path&gt; home/user1/File2.txt &lt;/path&gt;
              &lt;/file&gt;
          &lt;/children&gt;
          &lt;file&gt;
              &lt;name&gt; User1 &lt;/name&gt;
              &lt;path&gt; home/user1 &lt;/path&gt;
          &lt;/file&gt;
      &lt;/children&gt;
      &lt;children&gt;
          &lt;file&gt;
              &lt;name&gt; User2 &lt;/name&gt;
              &lt;path&gt; home/user2 &lt;/path&gt;
          &lt;/file&gt;
      &lt;/children&gt;
      &lt;children&gt;
          &lt;file&gt;
              &lt;name&gt; User3 &lt;/name&gt;
              &lt;path&gt; home/user3 &lt;/path&gt;
          &lt;/file&gt;
      &lt;/children&gt;
      &lt;children&gt;
          &lt;children&gt;
              &lt;file&gt;
                  &lt;name&gt; File4 &lt;/name&gt;
                  &lt;path&gt; home/user4/File4.txt &lt;/path&gt;
              &lt;/file&gt;
          &lt;/children&gt;
          &lt;file&gt;
              &lt;name&gt; User4 &lt;/name&gt;
              &lt;path&gt; home/user4 &lt;/path&gt;
          &lt;/file&gt;
      &lt;/children&gt;
      &lt;file&gt;
          &lt;name&gt; Home &lt;/name&gt;
          &lt;path&gt; /home &lt;/path&gt;
      &lt;/file&gt;
  &lt;/repositoryFileTreeDto&gt;
  </code></pre>
  
  <p>I want to print the Empty uses folders and Non-Empty User folders(i.e. users with 1 or more files). </p>
  
  <p>Here in the XML snippet. </p>
  
  <p>User 2 &amp; User 3 are Empty Folders and User 1 is a Non-Empty user. </p>
  
  <p>Condition to identify Empty and Non-Empty Users:</p>
  
  <p>If the User has any  tag at the same level then Non-Empty User. If the user doesn't have  tag then it is Empty User. </p>
  
  <p>Sample Code 1:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  import time
  import requests
  import csv
  tree = ET.parse('tree.xml')
  root = tree.getroot()
  for child in root.findall('children'):
      for subchlid in child.findall('file'):
          title = subchlid.find('title').text
          print(title)
      for subchlid1 in child.findall('children'):
          if subchlid1.tag == 'children':
              print(subchlid1.tag)
  </code></pre>
  
  <p>Code Output 1:</p>
  
  <pre><code>User1
  File1
  File2
  User2
  User3
  User4
  File4
  </code></pre>
  
  <p>Sample Code2:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  import time
  import requests
  import csv
  tree = ET.parse('tree.xml')
  root = tree.getroot()
  list_values = []
  dicts = {}
  for child in root.findall('children'):
      for sub_child in child.findall('file'):
          username = sub_child.find('title').text
  
      for sub_child1 in child.findall('children'):
          for sub_child2 in sub_child1.findall('file'):
              file_path = sub_child2.find('path').text
              file_title = sub_child2.find('title').text
          #print(username)
          #print(file_title)
          list_values.append(file_title)
          for user in username:
              dicts[username] = list_values
  print(dicts)
  </code></pre>
  
  <p>Code Output 2:</p>
  
  <pre><code>{'User1': ['File1', 'File2'],'User4': ['File1', 'File2', 'File4']}
  </code></pre>
  
  <p>Here in this output User2 and User3 is not part of the Dict because it is an empty folder and User4 is sharing the User1 files. </p>
  
  <p>Expected Output:</p>
  
  <pre><code>The number of Empty Users: 2
  The number of Non-Empty Users: 2
  User1 Files are: File1, File2
  User4 files are: File4
  </code></pre>
  
  <p>Thanks all guys.</p>