Build XML documents easily and concisely using native Python data structures.

pip install exemel==0.2.3



Build XML documents easily and concisely using native Python data structures.

Build Status


import exemel

xml ={
    'name': 'John Doe',
    'address': {
        'street': '1 Main St.',
        'city': 'Exampletown',
        'zip': 12345
    'phone': [
            '#text': '555-555-5555',
            '@type': 'mobile'
    'favorite': True
}, root='contact')

The return value would be an XML string equivalent to:

    <name>John Doe</name>
        <street>1 Main St.</street>
    <phone type="mobile">555-555-5555</phone>

The build function accepts a single positional argument: the dictionary-like object to convert into the root element. It also accepts two keyword arguments. The first, root, can be used to supply a tag for the root element. If the argument is not provided, the root element will be named 'root'. The second, encoding, determines the encoding of the resulting string. It defaults to a byte string if no argument is provided.


Any dictionary-like object (an instance of will be converted into an XML element. Each key in the dictionary is used to add sub-elements, attributes, or other properties to that element.

Any key that is not a decorator (see Decorators) will add one or more sub-elements.

xml ={
    'foo': 'bar'

NOTE: If using Python's dict type, which is unordered, the sub-elements in the resulting XML string will also be unordered. Order is often unimportant in XML aside from lists (see Iterables for ordering of list items). However, if ordering of sub-elements is important, you can use the collections.OrderedDict type.

ordered_dict = collections.OrderedDict()
ordered_dict['first'] = 1
ordered_dict['second'] = 2
xml =


Any non-string iterable (an instance of but not basestring) will be converted into ordered sibling XML elements with the same tag. List items may be basic literals or dictionaries (they can be mixed).

xml ={
    'myList': [
        {'type': 'complex'}

NOTE: Similarly to dictionaries, the order of the elements in the resulting XML string is dependent on the type of iterable. For example, an ordered iterable like a list will result ordered elements, but an unordered iterable like a set will result in unordered elements.


Any lxml.etree.Element instance used as a value in a Mapping or an item in an Iterable will be added to the document as-is.

Within a Mapping:

my_element = etree.Element('myElement')
my_element.text = 'my-text'

xml ={
    'myElement': my_element

Within an Iterable:

my_element = etree.Element('myList')
my_element.text = 'my-text'

xml ={
    'myList': [

NOTE: If the name of the Element instance does not match the expected name, exemel.MismatchedElementNameError will be raised. E.g.:{
    'oneName': etree.Element('anotherName')

would result in the following exception:

exemel.MismatchedElementNameError: Element with name 'anotherName' was added where name 'oneName' was expected


Decorators are special formats used in dictionary keys to add additional properties to an element.

Attribute Decorators

Any key prefixed with the @ character will add an attribute to the element.

xml ={
    '@foo': 'bar'
<root foo="bar"/>

If the value is None, the attribute will not be added. Otherwise, the value will be converted as described in Converting Text.

The Text Decorator

A key with the value #text can be used to add text to a complex element that couldn't be specified as just a string.

xml ={
    'example': {
        '@foo': 'bar',
        '#text': 'some text'
    <example foo="bar">some text</example>

If the value is None, no text will be added. Otherwise, the value will be converted as described in Converting Text.

The Namespace Decorator

Elements can be given a namespace by using the #ns decorator. Sub-elements will inherit the parent's namespace unless they specify their own namespace using the #ns decorator. Using None as the value sets the default namespace.

xml ={
    '#ns': 'foo',
    'one': None,
    'two': {
        '#ns': 'bar'
    'three': {
        '#ns': None
<f:root xmlns:f="foo">
    <b:two xmlns:b="bar"/>

Note that the namespace prefixes generated in the resulting XML string will be arbitrary. The prefixes used in the above example are simply for demonstration purposes.

Creating Empty Elements

An element with no text, sub-elements, attributes, or namespace can be created by using None or an empty dictionary as the value.

xml ={
    'none': None,
    'empty': {}

Converting Text

For attributes and text nodes, values will be converted to text as follows:

  • If the type is a boolean, values equal to True will become 'true', and values equal to False will become 'false'
  • All other types will attempt to be converted using the str function

Building an lxml.etree._Element instance

If you would like to use the lxml.etree API directly, you can build an lxml.etree._Element instance instead of a string by using exemel.build_element(). It's function signature is identical to that of


This library was inspired by xmlbuilder-js.