title: Recursively search for parent child combinations and build tree in python and XML
body: <p>I am trying to traverse this XML data full of parent->child relationships and need a way to build a tree. Any help will be really appreciated. Also, in this case, is it better to have attributes or nodes for the parent-->child relationship? </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;nodes&gt;
      &lt;node name="Car" child="Engine"/&gt;
      &lt;node name="Car" child="Wheel"/&gt;
      &lt;node name="Engine" child="Piston"/&gt;
      &lt;node name="Engine" child="Carb"/&gt;
      &lt;node name="Carb" child="Bolt"/&gt;
      &lt;node name="Spare Wheel"/&gt;
      &lt;node name="Bolt" child="Thread"/&gt;
      &lt;node name="Carb" child="Foat"/&gt;
      &lt;node name="Truck" child="Engine"/&gt;
      &lt;node name="Engine" child="Bolt"/&gt;
      &lt;node name="Wheel" child="Hubcap"/&gt;
  &lt;/nodes&gt;
  </code></pre>
  
  <p>On the Python Script, this is what i have. My brain is fried and I cannot get the logic going? please help</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  tree = ET.parse('rec.xml')
  root = tree.getroot()
  def find_node(data,search):
      #str = root.find('.//node[@child="1.2.1"]')
      for node in data.findall('.//node'):
          if node.attrib['name']==search:
              print('Child--&gt;', node)
  
  for nodes in root.findall('node'):
      parent = nodes.attrib.get('name')
      child = nodes.attrib.get('child')
      print (parent,'--&gt;', child)
      find_node(root,child)
  </code></pre>
  
  <p>A possible output that is expected is something like this (really dont care about the sorting order, As long as all node items are represented somewhere in the tree.  </p>
  
  <pre><code>Car --&gt; Engine --&gt; Piston
  Car --&gt; Engine --&gt; Carb --&gt; Float
  Car --&gt; Engine --&gt; Carb --&gt; Bolt --&gt; Thread
  Car --&gt; Wheel --&gt; Hubcaps
  Truck --&gt; Engine --&gt; Piston
  Truck --&gt; Engine --&gt; Carb --&gt; Bolt --&gt; Thread
  Truck --&gt; Loading Bin
  Spare Wheel --&gt;
  </code></pre>