{"id":2848,"library":"xsdata","title":"xsData: Python XML Binding","description":"xsData is a complete data binding library for Python (current version 26.2) that allows developers to access and use XML and JSON documents as simple objects rather than using DOM. It features a code generator for XML schemas, DTD, WSDL definitions, XML & JSON documents, producing simple dataclasses with type hints. It is actively maintained with frequent releases, typically monthly or bi-monthly.","status":"active","version":"26.2","language":"en","source_language":"en","source_url":"https://github.com/tefra/xsdata","tags":["xml","json","binding","xsd","wsdl","dataclasses","soap","codegen"],"install":[{"cmd":"pip install xsdata","lang":"bash","label":"Core library"},{"cmd":"pip install xsdata[cli,lxml,soap]","lang":"bash","label":"Full installation (CLI, lxml XML handler, SOAP client)"}],"dependencies":[{"reason":"Minimum Python version required.","package":"python","optional":false},{"reason":"Highly optimized XML parsing backend, included with `[lxml]` extra.","package":"lxml","optional":true},{"reason":"Default transport for SOAP webservice clients, included with `[soap]` extra.","package":"requests","optional":true},{"reason":"CLI entry point, included with `[cli]` extra.","package":"click","optional":true}],"imports":[{"symbol":"XmlParser","correct":"from xsdata.formats.dataclass.parsers import XmlParser"},{"symbol":"XmlSerializer","correct":"from xsdata.formats.dataclass.serializers import XmlSerializer"},{"note":"Classes are generated into a Python package specified by the `--package` CLI option.","symbol":"generated_model_classes","correct":"from {your_package_name} import {ClassName}"}],"quickstart":{"code":"from dataclasses import dataclass, field\nfrom datetime import date\nfrom xsdata.formats.dataclass.parsers import XmlParser\nfrom xsdata.formats.dataclass.serializers import XmlSerializer\nfrom xsdata.formats.dataclass.serializers.config import SerializerConfig\n\n@dataclass\nclass Person:\n    first_name: str = field(metadata=dict(name='FirstName'))\n    last_name: str = field(metadata=dict(name='LastName'))\n    birth_date: date = field(metadata=dict(name='BirthDate', type='Attribute', format='%Y-%m-%d'))\n\n@dataclass\nclass Family:\n    person: list[Person] = field(default_factory=list, metadata=dict(name='Person'))\n\n# Create an instance\nfamily_obj = Family(person=[\n    Person(first_name='John', last_name='Doe', birth_date=date(1980, 5, 15)),\n    Person(first_name='Jane', last_name='Doe', birth_date=date(1982, 11, 22))\n])\n\n# Serialize to XML\nconfig = SerializerConfig(pretty_print=True, xml_declaration=True, encoding='UTF-8')\nserializer = XmlSerializer(config=config)\nxml_output = serializer.render(family_obj)\nprint(xml_output)\n\n# Deserialize from XML\nxml_input = \"\"\"\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Family>\n  <Person BirthDate=\"1980-05-15\">\n    <FirstName>John</FirstName>\n    <LastName>Doe</LastName>\n  </Person>\n  <Person BirthDate=\"1982-11-22\">\n    <FirstName>Jane</FirstName>\n    <LastName>Doe</LastName>\n  </Person>\n</Family>\n\"\"\"\nparser = XmlParser()\ndeserialized_family = parser.from_string(xml_input, Family)\nprint(deserialized_family.person[0].first_name)\n","lang":"python","description":"This quickstart demonstrates creating a simple dataclass structure, then serializing a Python object to XML and deserializing XML back into Python objects. For complex schemas, `xsdata`'s CLI tool is typically used to generate dataclasses from XSD, DTD, or WSDL files."},"warnings":[{"fix":"Upgrade Python to 3.10 or newer.","message":"Support for Python 3.9 was dropped in version 26.1. Users on Python 3.9 must upgrade their Python interpreter to >=3.10 to use newer xsData versions.","severity":"breaking","affected_versions":">=26.1"},{"fix":"Use CLI options such as `--structure-style namespaces` or `--structure-style single-package` during code generation.","message":"Complex XML schemas can lead to circular import issues in the generated Python models, especially with the default `filenames` structure style. Consider using alternative structure styles like `namespaces`, `clusters`, or `single-package` to mitigate this.","severity":"gotcha","affected_versions":"all"},{"fix":"Set `ParserConfig.fail_on_converter_warnings = True` to treat conversion warnings as exceptions and enforce strict type validation during parsing.","message":"The parser's default behavior for type conversion is lenient, converting problematic values to `str` and emitting a `ConverterWarning` rather than raising an exception. This can mask data quality issues.","severity":"gotcha","affected_versions":"all"},{"fix":"Review generated models and parsing logic, especially for schemas with complex choices, after upgrading. Regenerate models and test parsing against your expected XML inputs.","message":"Recent versions (e.g., v26.2) have fixed issues where elements in the same choice but different branches were incorrectly merged as lists. This correction might alter the structure of generated dataclasses or the parsing behavior for existing XML documents if previous versions relied on the 'incorrect' merging logic.","severity":"breaking","affected_versions":">=26.2"},{"fix":"Replace `--ns-struct` with `--structure-style namespaces` when generating code from the command line.","message":"The `--ns-struct` CLI option has been deprecated. Its functionality is now provided by `--structure-style namespaces`.","severity":"deprecated","affected_versions":">=21.6"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}