Skip to main content

Python Architecture for XML Binding

Project description

Build status License Supported Python versions Code coverage

Python Architecture for XML Binding

paxb is a library that provides an API for mapping between XML documents and Python objects.

paxb library implements the following functionality:

  • Deserialize XML documents to Python objects

  • Validate deserialized data

  • Access and update Python object fields

  • Serialize Python objects to XML documents

paxb provides an efficient way of mapping between an XML document and a Python object. Using paxb developers can write less boilerplate code emphasizing on application domain logic.

Since paxb based on attrs library paxb and attrs API can be mixed together.


You can install paxb with pip:

$ pip install paxb



Documentation is available at Read the Docs.

Quick start

Suppose you have an xml document user.xml:

<?xml version="1.0" encoding="utf-8"?>
<doc:envelope xmlns=<s>""</s>
    <doc:user name=<s>"Alex"</s> surname=<s>"Ivanov"</s> age=<s>"26"</s>>

        <doc:birthdate year=<s>"1992"</s> month=<s>"06"</s> day=<s>"14"</s>/>


            <doc:passport series=<s>"3127"</s> number=<s>"836815"</s>/>

        <data:occupations xmlns:data=<s>""</s>>
            <data:occupation title=<s>"yandex"</s>>
            <data:occupation title=<s>"skbkontur"</s>>


To deserialize the document you could use xml library api to parse the document and then access and modify the parsed xml DOM manually. Such an imperative code has a lot of boilerplate operations that takes a lot of time and can lead to bugs. Instead you can use paxb api to write a declarative style code. All you need to describe field mappings and types, paxb will serialize and deserialize data for you:

import json
import re
from datetime import date

import attr
import paxb as pb

@pb.model(name='occupation', ns='data', ns_map={'data': ''})
class Occupation:
    title = pb.attr()
    address = pb.field()
    employees = pb.field(converter=int)

@pb.model(name='user', ns='doc', ns_map={'doc': ''})
class User:
    name = pb.attr()
    surname = pb.attr()
    age = pb.attr(converter=int)

    birth_year = pb.wrap('birthdate', pb.attr('year', converter=int))
    birth_month = pb.wrap('birthdate', pb.attr('month', converter=int))
    birth_day = pb.wrap('birthdate', pb.attr('day', converter=int))

    def birthdate(self):
        return date(year=self.birth_year, month=self.birth_month, day=self.birth_day)

    def birthdate(self, value):
        self.birth_year = value.year
        self.birth_month = value.month
        self.birth_day =

    phone = pb.wrap('contacts', pb.field())
    emails = pb.wrap('contacts', pb.as_list(pb.field(name='email')))

    passport_series = pb.wrap('documents/passport', pb.attr('series'))
    passport_number = pb.wrap('documents/passport', pb.attr('number'))

    occupations = pb.wrap(
        'occupations', pb.lst(pb.nested(Occupation)), ns='data', ns_map={'data': ''}

    citizenship = pb.field(default='RU')

    def check(self, attribute, value):
        if not re.match(r'\+\d{11,13}', value):
            raise ValueError("phone number is incorrect")

with open('user.xml') as file:
    xml =

Then the deserialized object can be modified and serialized back to xml document or converted to json format:

    user = pb.from_xml(User, xml, envelope='doc:envelope', ns_map={'doc': ''})
    user.birthdate = user.birthdate.replace(year=1993)

    with open('user.json') as file:
        json.dump(attr.asdict(user), file)

except (pb.exc.DeserializationError, ValueError) as e:
    print(f"deserialization error: {e}")


    "age": 26,
    "birth_day": 14,
    "birth_month": 6,
    "birth_year": 1993,
    "citizenship": "RU",
    "emails": ["", ""],
    "name": "Alexey",
    "occupations": [
            "address": "Moscow",
            "employees": 8854,
            "title": "yandex"
            "address": "Yekaterinburg",
            "employees": 7742,
            "title": "skbkontur"
    "passport_number": "836815",
    "passport_series": "3127",
    "phone": "+79204563539",
    "surname": "Ivanov"

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

paxb-0.3.0.tar.gz (11.2 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page