<p>I'm currently trying ElementTree and it looks fine, it escapes HTML entities and so on and so forth. Am I missing something truly wonderful I haven't heard of?</p>

<p>This is similar to what I'm actually doing:</p>

<pre><code>import xml.etree.ElementTree as ET
root = ET.Element('html')
head = ET.SubElement(root,'head')
script = ET.SubElement(head,'script')
script.set('type','text/javascript')
script.text = "var a = 'I love &amp;aacute; letters'"
body = ET.SubElement(root,'body')
h1 = ET.SubElement(body,'h1')
h1.text = "And I like the fact that 3 &gt; 1"
tree = ET.ElementTree(root)
tree.write('foo.xhtml')

# more foo.xhtml
&lt;html&gt;&lt;head&gt;&lt;script type="text/javascript"&gt;var a = 'I love &amp;amp;aacute;
letters'&lt;/script&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;And I like the fact that 3 &amp;gt; 1&lt;/h1&gt;
&lt;/body&gt;&lt;/html&gt;
</code></pre>

don't you actually want something like:
```
```
html(head(script(type='text/javascript', content='var a = ...')),
body(h1('And I like the fact that 3 < 1'), p('just some paragraph'))
```
```
I think I saw something like that somewhere. This would be wonderful.
EDIT:
Actually, I went and wrote a library today to do
just that
:
magictree
You can use it like this:
```
```
from magictree import html, head, script, body, h1, p
root = html(
         head(
           script('''var a = 'I love &amp;aacute; letters''', 
                  type='text/javascript')),
         body(
           h1('And I like the fact that 3 > 1')))

# root is a plain Element object, like those created with ET.Element...
# so you can write it out using ElementTree :)
tree = ET.ElementTree(root)
tree.write('foo.xhtml')
```
```
The magic in
```
magictree
```
lies in how the importing works: The
```
Element
```
factories are created when needed. Have a
look at the source
, it is
based on an answer to another StackOverflow question
.