ecoxipy.string_output - Building XML Strings

StringOutput creates strings of XML.

Usage Example:

>>> xml_output = StringOutput(check_well_formedness=True)
>>> from ecoxipy import MarkupBuilder
>>> b = MarkupBuilder(xml_output)
>>> xml = b[:'section'] (
...     b.section(
...         b.p('Hello World!'),
...         None,
...         b.p(u'äöüß'),
...         b.p(b & '<&>'),
...         b(
...             '<raw/>text', b.br,
...             (str(i) for i in range(3)), (str(i) for i in range(3, 6))
...         ),
...         b | '<This is a comment!>',
...         b['pi-target':'<PI content>'],
...         b['pi-without-content':],
...         attr='\'"<&>'
...     )
... )
>>> xml == u"""<?xml version="1.0"?>\n<!DOCTYPE section><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>"""
True
>>> from ecoxipy import XMLWellFormednessException
>>> def catch_not_well_formed(method, *args):
...     try:
...         return getattr(xml_output, method)(*args)
...     except XMLWellFormednessException as e:
...         print(e)
>>> t = catch_not_well_formed(u'document', u'1nvalid-xml-name', None, None, [], True, u'UTF-8')
The value "1nvalid-xml-name" is not a valid XML name.
>>> t = catch_not_well_formed(u'document', u'html', u'"', None, [], True, u'UTF-8')
The value "\"" is not a valid document type public ID.
>>> t = catch_not_well_formed(u'document', u'html', None, u'"\'', [], True, u'UTF-8')
The value "\"'" is not a valid document type system ID.
>>> t = catch_not_well_formed(u'element', u'1nvalid-xml-name', [], {})
The value "1nvalid-xml-name" is not a valid XML name.
>>> t = catch_not_well_formed(u'element', u't', [], {u'1nvalid-xml-name': u'content'})
The value "1nvalid-xml-name" is not a valid XML name.
>>> t = catch_not_well_formed(u'processing_instruction', u'1nvalid-xml-name', None)
The value "1nvalid-xml-name" is not a valid XML processing instruction target.
>>> t = catch_not_well_formed(u'processing_instruction', u'target', u'invalid PI content ?>')
The value "invalid PI content ?>" is not a valid XML processing instruction content because it contains "?>".
>>> t = catch_not_well_formed(u'comment', u'invalid XML comment --')
The value "invalid XML comment --" is not a valid XML comment because it contains "--".
class ecoxipy.string_output.StringOutput(entities=None, check_well_formedness=False)[source]

An ecoxipy.Output implementation which creates XML as strings.

Parameters:
  • entities – A mapping of characters to text to replace them with when escaping.
  • check_well_formedness (bool()) – The property check_well_formedness is determined by this value.
check_well_formedness[source]

If True the nodes will be checked for valid values.

static is_native_type(content)[source]

Tests if an object is a XMLFragment instance.

Returns:True for instances having XMLFragment as their class, False otherwise.
element(name, children, attributes)[source]

Creates an element string.

Returns:The element created.
Return type:XMLFragment
Raises ecoxipy.XMLWellFormednessException:
 If check_well_formedness is True and the name is not a valid XML name.
text(content)[source]

Creates text string.

Returns:The created text.
Return type:XMLFragment
comment(content)[source]

Creates a comment string.

Returns:The created comment.
Return type:XMLFragment
Raises ecoxipy.XMLWellFormednessException:
 If check_well_formedness is True and content is not valid.
processing_instruction(target, content)[source]

Creates a processing instruction string.

Returns:The created processing instruction.
Return type:XMLFragment
Raises ecoxipy.XMLWellFormednessException:
 If check_well_formedness is True and either the target or the content are not valid.
document(doctype_name, doctype_publicid, doctype_systemid, children, omit_xml_declaration, encoding)[source]

Creates a XML document.

Returns:The created document.
Return type:XMLDocument
Raises ecoxipy.XMLWellFormednessException:
 If check_well_formedness is True and the document type’s document element name is not a valid XML name, doctype_publicid is not a valid public ID or doctype_systemid is not a valid system ID.
fragment(children)[source]

Return a XML fragment created from the children.

Return type:XMLFragment
class ecoxipy.string_output.XMLFragment[source]

An XML Unicode string created by StringOutput.

class ecoxipy.string_output.XMLDocument[source]

An Unicode string representing a XML document created by StringOutput.

encoding[source]

The encoding of the document.

encoded[source]

The document encoded with encoding, a byte string.

The data of this property is created on first access, further retrieval of this property yields the same object.

Previous topic

ecoxipy.transformation - Transforming XML

Next topic

ecoxipy.etree_output - Building ElementTree Data