Skip to main content
# elementflow

[![Build Status](https://travis-ci.org/isagalaev/elementflow.png)](https://travis-ci.org/isagalaev/elementflow)

Elementflow is a Python library for generating XML as a stream. Some existing
XML producing libraries (like ElementTree, lxml) build a whole XML tree in
memory and then serialize it. It might be inefficient for moderately large XML
payloads (think of a content-oriented Web service producing lots of XML data
output). Python's built-in xml.sax.saxutils.XMLGenerator is very low-level and
requires closing elements by hand.

Also, most XML libraries, to be honest, suck when dealing with namespaces.


## Usage

Basic XML generation:

```python
import elementflow
file = open('text.xml', 'w') # can be any object with .write() method

with elementflow.xml(file, u'root') as xml:
xml.element(u'item', attrs={u'key': u'value'}, text=u'text')
with xml.container(u'container', attrs={u'key': u'value'}):
xml.text(u'text')
xml.element(u'subelement', text=u'subelement text')
```

Using `with` is required to properly close container elements. The library
expects unicode strings on input and produces utf-8 encoded output (you *may*
omit those "u"s for pure ASCII strings if you want to, Python will convert
them automatically).

XML with namespaces:

```python
with elementflow.xml(file, 'root', namespaces={'': 'urn:n', 'n1': 'urn:n1'}) as xml:
xml.element('item')
with xml.container('container', namespaces={'n2': 'urn:n2'):
xml.element('n1:subelement')
xml.element('n2:subelement')
```

Elements with namespaces are defined using prefixes. You can define namespaces
at the root level and for any container. The library will check for namespace
prefixes that wasn't defined beforehand and will raise ValueError in that case.

Pretty-printing is also supported:

```python
with elementflow.xml(file, 'root', indent=True):
# ...
```

In some cases it's more convenient to have such XML producer as a Python
iterator. This is easily done by wrapping XML generation code into a generator
function:

```python
def g():
xml = elementflow.xml(elementflow.Queue(), 'root')
with xml:
for item in collection:
xml.element(...)
yield xml.file.pop()
yield xml.file.pop()
```

`elementflow.Queue()` is a temporary buffer that accepts data from an XML
generator and is cleared upon calling .pop() on it. You also might want to
yield data from the iterator only when this buffer reaches a certain size:

```python
if len(xml.file) > BUFSIZE:
yield xml.file.pop()
```

Release files for elementflow 0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for elementflow 0.5
File Size Uploaded
elementflow-0.5.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for elementflow 0.5
File Interpreter ABI Platform
elementflow-0.5-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 12.2 kB

Release files / elementflow-0.5.tar.gz

Download URL elementflow-0.5.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
80d07550b92830e9f2ebdcda1b2106202ee9766e0e763b6489b9b2037bf9a445
BLAKE2b-256 checksum
How to use checksums
f7c31a91729731c2e71bce283fb081313bb5501fa9ba4f557992a3bc78e62639
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / elementflow-0.5-py2.py3-none-any.whl

Download URL elementflow-0.5-py2.py3-none-any.whl
Size 6.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
84b7cdaec6a9de075db9104fb5b1b829f8c4cb90ab86adc52c38eaa42e4f375e
BLAKE2b-256 checksum
How to use checksums
37bc81c3ddb79c4e82763ca459bc6a1518093e7021262a030e59cd448e06c8b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.5 This release

2 release files

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page