title: creating xml tree from a textfile with Python
body: <p>I need to avoid creating double branches in an xml tree when parsing a text file. Let's say the textfile is as follows (the order of lines is random):</p>
  
  <p>branch1:branch11:message11<br>
  branch1:branch12:message12<br>
  branch2:branch21:message21<br>
  branch2:branch22:message22</p>
  
  <p>So the resulting xml tree should have a root with two branches. Both of those branches have two subbranches. The Python code I use to parse this textfile is as follows:</p>
  
  <pre><code>import string
  fh = open ('xmlbasic.txt', 'r')
  allLines = fh.readlines()
  fh.close()
  import xml.etree.ElementTree as ET
  root = ET.Element('root')
  
  for line in allLines:
     tempv = line.split(':')
     branch1 = ET.SubElement(root, tempv[0])
     branch2 = ET.SubElement(branch1, tempv[1])
     branch2.text = tempv[2]
  
  tree = ET.ElementTree(root)
  tree.write('xmlbasictree.xml')
  </code></pre>
  
  <p>The problem with this code is, that a branch in xml tree is created with each line from the textfile.</p>
  
  <p>Any suggestions how to avoid creating another branch in xml tree if a branch with this name exists already?</p>