XML (and XHTML) Generation
==========================
The elements generator lets you easily create snippets of XML or XHTML:
>>> from cybertools.xml.element import elements as e
>>> doc = e.html(
... e.head(e.title(u'Page Title')),
... e.body(
... e.div(u'The top bar', class_=u'top'),
... e.div(u'The body stuff', class_=u'body'),
... ))
>>> print doc.render()
Page Title
The top bar
The body stuff
An XML element thus created may be converted to an ElementTree:
>>> doc.renderTree()
'Page TitleThe top bar
The body stuff
'
>>> tree = doc.makeTree()
>>> tree.findtext('head/title')
'Page Title'
>>> xml = ('Page Title'
... 'The top bar
'
... 'The body stuff
')
>>> from cybertools.xml.element import fromXML
>>> doc = fromXML(xml)
>>> print doc.render()
Page Title
The top bar
The body stuff
Alternative Notation
--------------------
We can also create such a structure by successively adding elements
just by accessing an element's attributes:
>>> doc = e.html
>>> dummy = doc.head.title(u'Page Title')
>>> body = doc.body
>>> div1 = body.div(u'The top bar', class_=u'top')
>>> div2 = body.div(u'The body stuff', class_=u'body')
>>> print doc.render()
Page Title
The top bar
The body stuff
>>> for text in (u'Welcome', u'home'):
... p = div2.p(text, style='font-size: 80%;')
>>> print doc.render()
.........