ecoxipy.etree_output - Building ElementTree Data

ETreeOutput creates xml.etree.ElementTree structures.

Usage Example:

>>> from xml.dom.minidom import getDOMImplementation
>>> etree_output = ETreeOutput()
>>> from ecoxipy import MarkupBuilder
>>> b = MarkupBuilder(etree_output)
>>> xml_doc = b[:'section':True] (
...     b.section(
...         b.p(b & 'Hello World!'),
...         None,
...         b(u'<p>äöüß</p>'),
...         b.p('<&>'),
...         b(
...             '<raw/>text', b.br,
...             (i for i in range(3)), (i for i in range(3, 6))
...         ),
...         b | '<This is a comment!>',
...         b['pi-target':'<PI content>'],
...         b['pi-without-content':],
...         attr='\'"<&>'
...     )
... )
>>> from io import BytesIO
>>> bytes_io = BytesIO()
>>> xml_doc.write(bytes_io, 'utf-8', True)
>>> document_string = u"""<?xml version='1.0' encoding='utf-8'?>\n<section attr="'&quot;&lt;&amp;&gt;"><p>Hello World!</p><p>äöüß</p><p>&lt;&amp;&gt;</p><raw />text<br />012345<!--<This is a comment!>--><?pi-target <PI content>?><?pi-without-content?></section>"""
>>> bytes_io.getvalue() == document_string.encode('UTF-8')
True
class ecoxipy.etree_output.ETreeOutput(element_factory=None)[source]

An Output implementation which creates xml.etree.ElementTree structures.

Parameters:element_factory – A xml.etree.ElementTree-compatible factory. If this is None xml.etree.ElementTree is used.
is_native_type(content)[source]

Tests if an object is a etree object by calling iselement() of the element factory.

Returns:True for compatible xml.etree.ElementTree objects, False otherwise.
element(name, children, attributes)[source]

Creates an element.

text(content)[source]

Creates an Unicode string.

comment(content)[source]

Creates a comment element.

processing_instruction(target, content)[source]

Creates a processing instruction element.

document(doctype_name, doctype_publicid, doctype_systemid, children, omit_xml_declaration, encoding)[source]

Creates an xml.etree.ElementTree.ElementTree-compatible object using the factory.

As xml.etree.ElementTree lacks support for document type declarations, the doctype_* parameters are ignored. Element tree wrappers do not allow specification of the output encoding and of the XML declaration, so encoding and omit_xml_declaration are also ignored. As element trees only allow one root element, the length of children must be zero or one, otherwise a ValueError is raised.

Previous topic

ecoxipy.string_output - Building XML Strings

Next topic

ecoxipy.dom_output - Building DOM Data