xsdata powered by pydantic!#

https://github.com/tefra/xsdata/workflows/tests/badge.svg https://readthedocs.org/projects/xsdata-pydantic/badge https://codecov.io/gh/tefra/xsdata-pydantic/branch/master/graph/badge.svg https://img.shields.io/github/languages/top/tefra/xsdata-pydantic.svg https://www.codefactor.io/repository/github/tefra/xsdata-pydantic/badge https://img.shields.io/pypi/pyversions/xsdata-pydantic.svg https://img.shields.io/pypi/v/xsdata-pydantic.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 pydantic!

Install#

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

Generate Models#

$ # Generate models
$ xsdata http://rss.cnn.com/rss/edition.rss --output pydantic
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
from dataclasses import field
from pydantic.dataclasses import dataclass

@dataclass
class Rss:
    class Meta:
        name = "rss"

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

...

XML Parsing#

>>> from xsdata_pydantic.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
"'A total lack of discipline': Clarissa Ward visits abandoned Russian foxholes"

>>> result.channel.item[2].pub_date
'Fri, 08 Apr 2022 22:56:33 GMT'
>>> result.channel.item[2].link
'https://www.cnn.com/videos/world/2022/04/08/ukraine-chernihiv-visit-ward-pkg-tsr-vpx.cnn'

Changelog: 22.10 (2022-10-02)#

  • Initial Release

Why Pydantic?#

Because you asked for! Pydantic offers out of the box validations and offers a dataclasses compatibility layer that we utilize to bring code generation and xml data binding with xsdata!

Limitations#

Pydantic dataclasses don’t behave with nested classes and self referencing types.

Check issue on github. Until this issue is fixed, you can use the xsdata generator –unnest-classes flag, that will move nested classes to the root level.

xsdata SOURCE --unnest-classes --output pydantic

There is still some issues with forward references but most of the xsdata-samples tests are passing.

The plugin is using xsdata’s data bindings to parse json/xml, only xsdata’s types are supported!