Skip to main content

Wrapper for ``lxml`` trees which serializes to string upon iteration.

Project description

Overview

This package provides a wrapper for lxml trees which serializes to string on iteration, but otherwise makes the tree available in an attribute.

The primary for this is WSGI middleware which may avoid needless XML parsing and serialization.

Usage

It’s recommend to use the lazy decorator on your application method. This allows you to return an lxml tree object, which is then automatically turned into an XMLSerializer.

>>> from repoze.xmliter import lazy
>>> @lazy
... def application(environ, start_response)
...     return some_lxml_tree

You may provide a serializer function, which will be used when the XMLSerializer is eventually iterated over (i.e. when the response is rendered):

>>> @lazy(serializer=lxml.html.tostring)
... def application(environ, start_response)
...     return some_lxml_tree

Middleware can use isinstance to test if the result is an XML iterable:

>>> from repoze.xmliter.serializer import XMLSerializer
>>> isinstance(result, XMLSerializer)

In this case, the middleware can simply access the tree attribute of the result.

There are two convenience methods which can be used to parse a WSGI iterable of strings and build an XMLSerializer object, but avoids re-building the serializer if the input iterable is already an instance of XMLSerializer:

>>> from repoze.xmliter.utils import getXMLSerializer
>>> result = getXMLSerializer(result)

Or, if you are parsing HTML:

>>> from repoze.xmliter.utils import getHTMLSerializer
>>> result = getHTMLSerializer(result)

If result is not an XMLSerializer, it will be parsed using a feed parser, turned into an lxml tree, and wrapped up in an XMLSerializer, which is returned.

Changelog

2.0 (2026-05-08)

  • Make final release, no code changes. [maurits]

2.0a2 (2025-12-22)

  • Let getXMLSerializer raise an XMLSyntaxError when passed an empty iterable. Then the result with lxml 6 is the same as it was with lxml 5, instead of throwing an AttributeError. [maurits]

2.0a1 (2025-12-22)

  • Replace pkg_resources namespace with PEP 420 native namespace. [maurits]

1.0 (2025-10-29)

  • Add support for Python 3.8 - 3.14. [maurits]

  • Drop support for Python 3.7: it is almost two years out of support and cannot easily be tested. [maurits]

1.0b1 (2024-01-31)

  • Fix serialization of a tree to bytes PR. [maurits]

  • Removed unused future dependency. Fixes issue 10. [maurits]

  • Drop support for Python 2.6, 2.7, 3.3, 3.4, 3.5, and 3.6. [tseaver, mborch]

  • Add support for Python 3.7, 3.8, 3.9, 3.10, and 3.11. [tseaver, mborch]

0.6.1 (2022-01-14)

  • Fixed tests with lxml 4.7.1 or higher. Fixes issue 8. [maurits]

0.6 - 2014-09-21

  • Python 3 compatibility [Lennart Regebro]

0.5 - 2012-01-25

  • Add __len__ to serializer to help WSGI servers. [Laurence]

  • Serializer should iter the entire string in one go. [Laurence]

0.4 - 2011-06-16

  • Ensure trailing space is removed when replacing doctype with empty string. [Laurence]

0.3 - 2011-06-03

  • Add doctype option to replace doctype on serialization. [Laurence]

0.2 - 2010-09-11

  • Use document encoding by default. (This fixes test failure on Ubuntu 10.04.) [Laurence]

  • Defer to xsl:output settings when serializing an XSLResultTree. [Laurence]

  • Turn off pretty printing by default for HTML to avoid affecting rendering on the browser. [Laurence]

0.1 - 2010-04-21

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

repoze_xmliter-2.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

repoze_xmliter-2.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file repoze_xmliter-2.0.tar.gz.

File metadata

  • Download URL: repoze_xmliter-2.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for repoze_xmliter-2.0.tar.gz
Algorithm Hash digest
SHA256 ebe7ee469b4558c45ac0040b312bc277d5d740b4c7e507d24427b75e78f88346
MD5 2ae85fd7c2d612cf083741a3b435794c
BLAKE2b-256 220ba3660a53c12ed9cb76f7c19231e74086067cb57e011b8933dc995b466e47

See more details on using hashes here.

File details

Details for the file repoze_xmliter-2.0-py3-none-any.whl.

File metadata

  • Download URL: repoze_xmliter-2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for repoze_xmliter-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4542fc36baf763a6c71b459a6068cf8728caf915567a7fcc342f0a5c064762bb
MD5 c8dbd41543077e9da4cd3b120a169f7f
BLAKE2b-256 c2a456a104eee1ffec1331b2265a64883e97a3990ab1d2c9fefcc6ef5ebecec7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page