title: Creating xml dynamically with lxml
body: <p>I havent used lxml to create xml, so i am somewhat lost. I can make a function, that creates an elemnt:</p>
  
  <pre><code>from lxml import etree as ET    
  from lxml.builder import E
  
  In [17]: def func():
      ...:     return E("p", "text", key="value")
  
  In [18]: page = (
      ...:     E.xml(
      ...:         E.head(
      ...:             E.title("This is a sample document")
      ...:         ),
      ...:         E.body(
      ...:             func()
      ...:             
      ...:         )
      ...:     )
      ...: )
  
  In [19]: print ET.tostring(page,pretty_print=True)
  &lt;xml&gt;
    &lt;head&gt;
      &lt;title&gt;This is a sample document&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;p key="value"&gt;text&lt;/p&gt;
    &lt;/body&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p>How can i make the function to add multiple elements? For example, i would like <code>func(3)</code> to create 3 new paragraphs. If the func reurns a list, i get a TypeError.</p>