Package lxml :: Module builder :: Class ElementMaker
[hide private]
[frames] | no frames]

Class ElementMaker

source code

object --+

Element generator factory.

Unlike the ordinary Element factory, the E factory allows you to pass in more than just a tag and some optional attributes; you can also pass in text and other elements. The text is added as either text or tail attributes, and elements are inserted at the right spot. Some small examples:

>>> from lxml import etree as ET
>>> from lxml.builder import E

>>> ET.tostring(E("tag"))
>>> ET.tostring(E("tag", "text"))
>>> ET.tostring(E("tag", "text", key="value"))
'<tag key="value">text</tag>'
>>> ET.tostring(E("tag", E("subtag", "text"), "tail"))

For simple tags, the factory also allows you to write E.tag(...) instead of E('tag', ...):

>>> ET.tostring(E.tag())
>>> ET.tostring(E.tag("text"))
>>> ET.tostring(E.tag(E.subtag("text"), "tail"))

Here's a somewhat larger example; this shows how to generate HTML documents, using a mix of prepared factory functions for inline elements, nested E.tag calls, and embedded XHTML fragments:

# some common inline elements
A = E.a
I = E.i
B = E.b

def CLASS(v):
    # helper function, 'class' is a reserved word
    return {'class': v}

page = (
            E.title("This is a sample document")
            E.h1("Hello!", CLASS("title")),
            E.p("This is a paragraph with ", B("bold"), " text in it!"),
            E.p("This is another paragraph, with a ",
                A("link", href=""), "."),
            E.p("Here are some reserved characters: <spam&egg>."),
            ET.XML("<p>And finally, here is an embedded XHTML fragment.</p>"),

print ET.tostring(page)

Here's a prettyprinted version of the output from the above script:

    <title>This is a sample document</title>
    <h1 class="title">Hello!</h1>
    <p>This is a paragraph with <b>bold</b> text in it!</p>
    <p>This is another paragraph, with <a href="">link</a>.</p>
    <p>Here are some reserved characters: &lt;spam&amp;egg&gt;.</p>
    <p>And finally, here is an embedded XHTML fragment.</p>

For namespace support, you can pass a namespace map (nsmap) and/or a specific target namespace to the ElementMaker class:

>>> E = ElementMaker(namespace="http://my.ns/")
>>> print(ET.tostring( E.test ))
<test xmlns="http://my.ns/"/>

>>> E = ElementMaker(namespace="http://my.ns/", nsmap={'p':'http://my.ns/'})
>>> print(ET.tostring( E.test ))
<p:test xmlns:p="http://my.ns/"/>
Instance Methods [hide private]
__init__(self, typemap=None, namespace=None, nsmap=None, makeelement=None)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
__call__(x, ...)
source code
__getattr__(self, tag) source code
x.__getattribute__('name') <==>
source code
a new object with type S, a subtype of T
__new__(T, S, ...) source code
helper for pickle
source code
__setstate__(...) source code

Inherited from object: __delattr__, __format__, __hash__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, typemap=None, namespace=None, nsmap=None, makeelement=None)

source code 
x.__init__(...) initializes x; see help(type(x)) for signature
Overrides: object.__init__


source code 
x.__getattribute__('name') <==>
Overrides: object.__getattribute__

__new__(T, S, ...)

source code 
Returns: a new object with type S, a subtype of T
Overrides: object.__new__


source code 
helper for pickle
Overrides: object.__reduce__
(inherited documentation)