dexml: a dead-simple Object-XML mapper for Python
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 dexml2 >>> from dexml2 import fields >>> class Person(dexml2.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(dexml2.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
Metadata
Release files for dexml2 0.5.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dexml2-0.5.3.tar.gz | 25.3 kB | Without provenance | Details for dexml2-0.5.3.tar.gz |
Release files / dexml2-0.5.3.tar.gz
| Download URL | dexml2-0.5.3.tar.gz |
|---|---|
| Size | 25.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b65dc599cd8d5f859a97dc00f58984d41da6ec054e6482cba0cf7cb59530c6da
|
|
BLAKE2b-256 checksum How to use checksums |
43c9f87f286d39551e5d3051c2792ad09486a361faf72804b7d3bb21e8436cfa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for dexml2 0.5.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.