Where's a DOM builder that uses the Builder Pattern to ... build DOMs?

Stephen Hansen apt.shansen at gmail.com
Thu Jan 7 20:39:30 EST 2010


On Thu, Jan 7, 2010 at 8:44 AM, Phlip <phlip2005 at gmail.com> wrote:

> On Jan 7, 5:36 am, Stefan Behnel <stefan... at behnel.de> wrote:
>
> > Well, then note that there are tons of ways to generate XML with Python,
> > including the one I pointed you to.
>
>         from lxml.html import builder as E
>        xml = E.foo()
>
> All I want is "<foo/>", but I get "AttributeError: 'module' object has
> no attribute 'foo'".
>
> A peek at dir(E) shows it only has HTML tags, all hard coded.
>
> So how to get it to generate any random XML tag my clients think of?
>

If you want to generate random XML, don't use the HTML sub-module of lxml.
Its a specific sub-set of functionality for HTML only documents.

>>> from lxml.builder import ElementMaker
>>> from lxml import etree
>>> E = ElementMaker()
>>> html = E.html(
...     E.form(
...             E.input(type="text", name="email"),
...             E.input(type="text", name="blah")
...     )
... )
>>> etree.tostring(html)
'<html><form><input type="text" name="email"/><input type="text"
name="blah"/></form></html>'

HTH,

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100107/24f28a56/attachment-0001.html>


More information about the Python-list mailing list