Skip to main content

a dead-simple Object-XML mapper for Python

Project description

Let’s face it: xml is a fact of modern life. I’d even go so far as to say that it’s good at what is does. But that doesn’t mean it’s easy to work with and it doesn’t mean that we have to like it. Most of the time, XML just needs to get out of the way and let you do some actual work instead of writing code to traverse and manipulate yet another DOM.

The dexml module takes the obvious mapping between XML tags and Python objects and lets you capture that as cleanly as possible. Loosely inspired by Django’s ORM, you write simple class definitions to define the expected structure of your XML document. Like so:

>>> import dexml
>>> from dexml import fields
>>> class Person(dexml.Model):
...   name = fields.String()
...   age = fields.Integer(tagname='age')

Then you can parse an XML document into an object like this:

>>> p = Person.parse("<Person name='Foo McBar'><age>42</age></Person>")
>>> p.name
u'Foo McBar'
>>> p.age
42

And you can render an object into an XML document like this:

>>> p = Person(name="Handsome B. Wonderful",age=36)
>>> p.render()
'<?xml version="1.0" ?><Person name="Handsome B. Wonderful"><age>36</age></Person>'

Malformed documents will raise a ParseError:

>>> p = Person.parse("<Person><age>92</age></Person>")
Traceback (most recent call last):
    ...
ParseError: required field not found: 'name'

Of course, it gets more interesting when you nest Model definitions, like this:

>>> class Group(dexml.Model):
...   name = fields.String(attrname="name")
...   members = fields.List(Person)
...
>>> g = Group(name="Monty Python")
>>> g.members.append(Person(name="John Cleese",age=69))
>>> g.members.append(Person(name="Terry Jones",age=67))
>>> g.render(fragment=True)
'<Group name="Monty Python"><Person name="John Cleese"><age>69</age></Person><Person name="Terry Jones"><age>67</age></Person></Group>'

There’s support for XML namespaces, default field values, case-insensitive parsing, and more fun stuff. Check out the documentation on the following classes for more details:

Model:

the base class for objects that map into XML

Field:

the base class for individual model fields

Meta:

meta-information about how to parse/render a model

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

dexml3-1.0.1.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

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

dexml3-1.0.1-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

Details for the file dexml3-1.0.1.tar.gz.

File metadata

  • Download URL: dexml3-1.0.1.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for dexml3-1.0.1.tar.gz
Algorithm Hash digest
SHA256 61db33d510be3b7fba72c6d8a1c3bacffa5f32b64b7a646ad97b20d1f537f140
MD5 800375aa3291eeb1f89e9e74d5c556f7
BLAKE2b-256 aed3bed1a6bc4dbbf5ae4b6b42784372253665a77d8bc165bf74fafe1688ce1c

See more details on using hashes here.

File details

Details for the file dexml3-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: dexml3-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for dexml3-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2b9693ac2902c5f4541163d02b2fa1b84715119fc7a2ba92381d38e7f17ab7b5
MD5 9bdf518c3905231d2cd0de54c8607c5e
BLAKE2b-256 9aec3d6d34e89c994c63421eb1dc5dd2c175ff6c2f57e74918afcab98ec96aba

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