This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!
Project Description
iXML - A simple iterative event-driven XML parser

.. image::

.. image::

.. image::

.. image::

iXML is an iterative event-driven XML parser with a standard Python iterator interface.




From PyPI (stable)::

pip install ixml

From Github (unstable)::

pip install git+git://

iXML currently requires the `lxml <>`_ library because there is no fallback backend based on the standard library yet.

Main API

- ``ixml.parse(data)``: iterator returning parsing events.

- ``ixml.items(data, path, builder_klass=DictObjectBuilder)``: iterator returning Python objects found under a specified path.


- ``data`` must be a file like object.

- The Python objects yielded by ``ixml.items`` are constructed from the parsing events by an ``ObjectBuilder`` (``DictObjectBuilder`` by default). Please make your own if you wish as long as it implements the ``ObjectBuilder`` interface (see ````).

- Top-level ``ixml`` module tries to automatically find and import a suitable parsing backend. You can also explicitly import a required backend from ``ixml.backends``.

Usage and examples

All examples will be using this XML document:

.. code:: python

>>> XML = '''<?xml version="1.0" encoding="utf-8"?>
<city name="Paris">
<monument>Eiffel Tower</monument>
<monument>Triumphal Arch</monument>
<museum>Louvre Museum</museum>
<museum>Quai Branly Museum</museum>
<city name="Dallas">
<monument>Bank America Plaza</monument>
<monument>Dallas Theatre Center</monument>
<museum>Dallas Museum of Art</museum>
<museum>Old Red Museum</museum>

- **ixml.parse**

Using the ``parse`` function, you can react on individual events:

.. code:: python

>>> import ixml
>>> from StringIO import StringIO

# The parse function takes a file like object
>>> data = StringIO(XML)

# Extract only the languages and the countries
>>> languages, countries = set(), set()
>>> for path, event, value in ixml.parse(data):
... if path == '':
... languages.add(value)
... elif path == '':
... countries.add(value)
>>> print languages, countries
set(['French', 'English']) set(['USA', 'France'])

Below are all the parsing events from ``parse``:

.. code:: python

('cities', u'start', None)
('', u'start', None)
('', 'data', 'Paris')
('', 'data', 'France')
('', 'data', 'French')
('', u'start', None)
('', 'data', 'Eiffel Tower')
('', 'data', 'Triumphal Arch')
('', 'data', 'Louvre Museum')
('', 'data', 'Quai Branly Museum')
('', u'end', None)
('', u'end', None)
('', u'start', None)
('', 'data', 'Dallas')
('', 'data', 'USA')
('', 'data', 'English')
('', u'start', None)
('', 'data', 'Bank America Plaza')
('', 'data', 'Dallas Theatre Center')
('', 'data', 'Dallas Museum of Art')
('', 'data', 'Old Red Museum')
('', u'end', None)
('', u'end', None)
('cities', u'end', None)

- **ixml.items**

Another usage is having iXML yields native Python objects for a specific path with ``items``:

.. code:: python

>>> import ixml
>>> from StringIO import StringIO

# The items function takes a file like object
>>> data = StringIO(XML)

>>> for city in ixml.items(data, ''):
... do_something_with(city)

Below are the two "city" Python objects created. They are constructed as a dict by default.
You can change this behavior by providing another builder class to the ``items`` function.

.. code:: python

'country': 'France',
'@name': 'Paris',
'language': 'French',
'attractions': {
'museum': ['Louvre Museum', 'Quai Branly Museum'],
'monument': ['Eiffel Tower', 'Triumphal Arch']
'country': 'USA',
'@name': 'Dallas',
'language': 'English',
'attractions': {
'museum': ['Dallas Museum of Art', 'Old Red Museum'],
'monument': ['Bank America Plaza', 'Dallas Theatre Center']

Parsing events

Parsing events contain the XML tree context (path), an event and a value::

(path, event, value)

1. **The tree context (or path)**

It is a simplified path format that:

- uses dots to define different levels
- uses namespace prefixes in the tag name
- ignores default namespaces (handled automatically behind the scene)
- uses @ for attributes



2. **The events**

- "start" and "end" for containers:

.. code:: python

<rss> # => ('rss', 'start', None)
</rss> # => ('rss', 'end', None)

- "data" for leaves and attributes:

.. code:: python

<title myAttr="Test">Some text</title> # => ('rss.title', 'data', 'Some text'), ('rss.title.@myAttr', 'data', 'Test')

3. **The value**

If there is a value, it will always be a string, None otherwise.
There is currently no automatic conversion feature (to int, etc).


iXML can provide several implementation of the parsing by using backends located in ixml/backends:

- ``lxmliterparse``: wrapper around the well known `iterparse LXML <>`_ function.

You can import a specific backend and use it in the same way as the top level library:

.. code:: python

>>> import ixml.backends.lxmliterparse as ixml
>>> for path, event, value in ixml.parse(...):
... # ...

Importing the top level library as ``import ixml`` tries to import all backends in order.

iXML currently requires the `lxml <>`_ library because there is no fallback backend based on the standard library yet.

The ``items`` function uses an ObjectBuilder to build an object while parsing the data.

The events are passed into the ``event`` function of the builder that accepts three parameters: path, event type and value.
The object being built is available at any time from the ``value`` attribute.

You can make your own builder as long as it implements the ObjectBuilder interface (see ixml/builders/interface).


Clone and install testing dependencies::

$ python develop
$ pip install -r requirements_tests.txt

Ensure tests pass::

$ ./scripts/

Or using tox::

$ tox
Release History

Release History


This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date (17.6 kB) Copy SHA256 Checksum SHA256 Source Aug 17, 2013

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting