title: How can I make xml from complex dictionary in Python?
body: <p>I want to make recursive function to generate xml from complex dictionary (i.e. dictionary of dictionaries, for example</p>
  <p><code>dataDict = {'item1': 'this in item1', 'item2': 'this in item2', 'item3': 'this is item3'} rootDict = {'name': 'Daud', 'code': 'MC111011', 'data': dataDict}</code>).</p>
  <p>This is what I have tried,</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>from lxml import etree
  
  def dict_to_xml(dictData):
      for key,val in dictData.items():
          if isinstance(val, type(dict)):
              child = dict_to_xml(val)
              if child is not None:
                  yield from child
          else:
              yield key, val
              child = etree.Element(key)
              child.text = str(val)
          return child
  
  
  # create XML
  dataDict = {'item1': 'this in item1', 'item2': 'this in item2', 'item3': 'this is item3'}
  rootDict = {'data': dataDict}
  for key, value in rootDict.items():
      if isinstance(value, type(dict)):
          childElement = dict_to_xml(value)
      else:
          childElement = etree.Element(key)
          childElement.text = str(value)
  
  
  root = etree.Element('root')
  root.append(etree.Element('child'))
  # another child with text
  
  root.append(childElement)
  
  
  # pretty string
  s = etree.tostring(root, pretty_print=True)
  print(s)</code></pre>
  </div>
  </div>
  </p>
  <p>I am getting the following output</p>
  <pre><code>b&quot;&lt;root&gt;\n  &lt;child/&gt;\n  &lt;data&gt;{'item1': 'this in item1', 'item2': 'this in item2', 'item3': 'this is item3'}&lt;/data&gt;\n&lt;/root&gt;\n&quot;
  </code></pre>
  <p>What am I missing?</p>