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
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.html( E.head( E.title("This is a sample document") ), E.body( 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="http://www.python.org"), "."), 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::
<html> <head> <title>This is a sample document</title> </head> <body> <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="http://www.python.org">link</a>.</p> <p>Here are some reserved characters: <spam&egg>.</p> <p>And finally, here is an embedded XHTML fragment.</p> </body> </html>
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/"/> intan integer is required<cyfunction %U at %p>__loader__loader__file__origin__package__parent__path__submodule_search_locationsPyObject *name '%U' is not definedcannot import name %Sstringsourcesrc/lxml/builder.pyExpected %.16s, got %.200stupleexactly__pyx_unpickle_ElementMakeradd_cdataadd_text%d.%d%s_cython_0_29_14builtinscython_runtime__builtins__init lxml.builderat leastat most__init__add_dict__call__getcython_function_or_method__reduce____module__func_doc__doc__func_name__name____qualname____self__func_dict__dict__func_globals__globals__func_closure__closure__func_code__code__func_defaults__defaults____kwdefaults____annotations__lxml.builder.ElementMaker_nsmap_typemap_namespace_makeelement__getattr____reduce_cython____setstate_cython__HE��(E�� E��@E��0E������{���{�����l~��X���H���(���p�������̝�����������H���С��r���i���`���Incompatible checksums (%s vs 0x9fc2fa1 = (_makeelement, _namespace, _nsmap, _typemap))Can't add a CDATA section. Element already has some text: %r The ``E`` Element factory for generating XML documents. ElementMaker.__setstate_cython__ElementMaker.__reduce_cython____pyx_unpickle_ElementMaker__init__.<locals>.add_cdata__init__.<locals>.add_text__init__.<locals>.add_dictbad argument type: %s(%r)src/lxml/builder.pycline_in_traceback__setstate_cython____pyx_PickleError__reduce_cython__use_setstatestringsource__pyx_checksumlxml.builderElementMakermakeelementPickleError__pyx_resultlxml.etreebasestringValueErrorIndexError__reduce_ex____pyx_statenamespaceiselementfunctoolsadd_cdataTypeErrorNameError__setstate____pyx_type__pyx_capi____getstate__add_textadd_dictunicodetypemappartialElementupdate__reduce__pickle__import___dictattribappendstatensmapitemscdataCDATAtext__test__tailself__name____main__itemelem__dict__tag__new____mro__get*}{ETvkE;�U����� �����<�������H@��p j��� l���>3���4��|4��(,5��L<5��`L5��t\5���l5����5����5���,6���|6����6���6���6��0�6��D7��X|7����7���<8����8����8��,9��0�:��pl?����@����@����A��H�B����B����B���lE����E��F�� <F��x<G���I��J����J���K���LL��0 �L��� LM��� �M��� LN��� �N�� LO��( �O��H P��\ \P��x Q��� lQ��� �R��\�R��x�S����V��4lX���LY���Z����Z���[��4 |[��` �\��� |]��� �d��� \g��8j��|�m����m���Ln���t��|�u���̀��̅��\L���X���������XzRx�$�� FJw�?:*3$"D��\1��XHu C xL1���E�f EJ F��1�� ��1�� ��1�� ��1�� ��1���1��5dP�1��\,H2��IlPD�2�� X|2��$l�2�� ��2����2��$0��2��hA�F�G A CAEKCA(��2��YE�A�D T DAE(3��YE�A�D T DAE4P3��wH�3��3Y�Yd�3��0Y�V<��3���A�J0I DHD GEZ CCV FDL��5��iB�E�B �B(�D0�D8�H�o 8A0A(B BBBD �9��jy�D R AA4�:��LH�:���B�B�B �A(�A0�p (A BBBAY (A BBBH@��;���B�E�B �D(�D0�G@r 0A(A BBBF�<��7Y�]�<<��,8<��lF�A�A �� CBE<x>��P�>��uE�K H[p�>��-E�c8����{B�E�E �G(�A0�](C BBB ��>���E�� Of$����SA�D�A JAA8p?���B�E�D �D(�GP (A ABBHDPA��B�E�B �E(�D0�A8�DP�8D0A(B BBB8����fF�E�A �A(�A0N(D ABB4��A��eF�D�D �E GBDAAB4�A��|A�A�D | AADO AAG8DB��:F�B�B �D(�D0� (A BBB�C��iM�A�A �zRx� ���$$���* ABAWAB�C���HI G�C��|Hf B �C��|Hf B<XD���Ho IX�D��pHo IoxE��pHo Io�lE��H��E��Lf�YA�8��E���b�B�D �D(�J@U (A ABBGPF��[`�kE�( ���B�K�K ��AB\LhF��"B�B�D �A(�D0R (D ABBHN (D ABBI�(D DBB�8G��3A�U JD�\G���s�C�A �e �D�B�NAABD���h ���p H��B�B�E �A(�C0�� (A BBBEA (A BBBHG8T@AHBPBXA`AhApAxA�I0\� �J��hB�B�E �B(�D0�A8�G@WHJPGXC`AhBpI@o 8D0A(B BBBF� �K���� �L���( XM��mA�D�G e DAJH8 �M��tB�H�D �D(�D0s (D ABBFN(D ABB(� �M��pA�D�D ^ DAD8� N��.B�B�A �A(�D0� (A ABBA� O���H�O��qF�B�B �B(�A0�I8�D@� 8D0A(B BBBH8L�V��_B�E�D �A(�GP� (A ABBG@�Y���B�B�B �A(�A0�G@� 0D(A BBBFH��[���B�J�E �E(�A0�C8�D` 8A0A(B BBBD_�� 4,_��OF�D�D �Q ABFYGBdd0_��pB�B�B �B(�A0�A8�D@� 8D0A(B BBBJ