xsdata powered by attrs!

https://github.com/tefra/xsdata/workflows/tests/badge.svg https://readthedocs.org/projects/xsdata-attrs/badge https://codecov.io/gh/tefra/xsdata-attrs/branch/master/graph/badge.svg https://img.shields.io/github/languages/top/tefra/xsdata-attrs.svg https://www.codefactor.io/repository/github/tefra/xsdata-attrs/badge https://img.shields.io/pypi/pyversions/xsdata-attrs.svg https://img.shields.io/pypi/v/xsdata-attrs.svg

xsData is a complete data binding library for python allowing developers to access and use XML and JSON documents as simple objects rather than using DOM.

Now powered by attrs!

Install

$ # Install with cli support
$ pip install xsdata-attrs[cli]

Generate Models

$ # Generate models
$ xsdata http://rss.cnn.com/rss/edition.rss --output attrs
Parsing document edition.rss
Analyzer input: 9 main and 0 inner classes
Analyzer output: 9 main and 0 inner classes
Generating package: init
Generating package: generated.rss
...

@attr.s
class Rss:
    class Meta:
        name = "rss"

    version: Optional[float] = attr.ib(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )
    channel: Optional[Channel] = attr.ib(
        default=None,
        metadata={
            "type": "Element",
        }
    )

...

XML Parsing

>>> from xsdata_attrs.bindings import XmlParser
>>> from urllib.request import urlopen
>>> from generated.rss import Rss
>>>
>>> parser = XmlParser()
>>> with urlopen("http://rss.cnn.com/rss/edition.rss") as rq:
...     result = parser.parse(rq, Rss)
...
>>> result.channel.item[2].title
'Vatican indicts 10 people, including a Cardinal, over an international financial scandal'
>>> result.channel.item[2].pub_date
'Sat, 03 Jul 2021 16:37:14 GMT'
>>> result.channel.item[2].link
'https://www.cnn.com/2021/07/03/europe/vatican-financial-scandal-intl/index.html'

Why attrs?

Attrs is the original python data class without boilerplate with a huge following. It comes with features not found in the stdlib dataclasses, like keyword only arguments and slotted classes, that won’t be available until Python 3.10

It’s so similar to dataclasses that also makes it the perfect candidate to test the xsdata’s plugin hooks.