Skip to main content

Simple, fast, lightweight xml parser and validator defined by classes.

Project description

XML Classes

Simple, fast, lightweight xml parser and validator defined by classes, and useful error messages.

Reason to pick this over other xml parsers:

  • Non alias. (Harder to match tag/attribute name with the class variable name)
  • Need advanced features. (Like default factory, custom decoder, custom encoder etc.)

TODO: Speed test against pydantic-xml and xmltodict with pydantic.

Basic examples:

For the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<root value="data" />

The following python class can be used to parse it:

class RootAttribute(XmlClass):
    value: str

It automatically assumes it is an xml tag attribute.

And then parse it:

    root = RootAttribute.from_string(xml_string)

For the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<root>
    <value value="data"/>
</root>

The following python class can be used to parse it:

class Value(XmlClass):
    value: str

class RootElement(XmlClass):
    value: Value

When the type is set to be of XmlClass, it will be parsed as a sub element.

For xml text field, it is a bit more complex. For the following XML: """

"""

The following python class can be used to parse it:

class Value(XmlClass):
    data: XmlTextField[str]

When the type is set to be of XmlTextField, it will be parsed as a text field, where the text-field is of the type given in the square brackets.

Supported Annotations type:

  • int
  • float
  • str
  • bool 1
  • None
  • datetime.datetime 2
  • uuid.UUID
  • pathlib.Path
  • typing.Any 3
  • Literal
  • Enum
  • list 4
  • tuple 4
  • union
  • XmlClass
  • XmlTextField

Other Info:

Trailing underscore

Trailing underscore is ignored when mapping from XML to python class. It is used in the casses where a child element tag and a attribute have the same name. Through not a requirement, the trailing underscore should be used in the attribute name.

Dash in tag or Attribute names

Since dash in python name is not allowed, dash in tag or attribute name is replaced with underscore.

NOTE:

  • This is a work in progress, ATM it only parses the XML data, to a python class.
  • UNSURE: Do None type hint as the only one even make sense?
  • UNSURE: Should null value be None type hint, and something else for optional?

Tests TODO:

  • Test XmlClass | None case, where there is parsing error in XmlClass.
  • Test OneTwo: typing.Literal["firstPasser", "secondPasser"] | None case, where it do not Exist.
  • Test xmlClass | AnotherXmlClass case, with different Names.

TODO:

  • Make the error messages better for when there is a error deep inside a xmlClass.
  • Make a test for typing.Any.
  • Added defusedxml as dependency. (Security reasons)
  • Fix ALL type hints...
  • Add support for decoder and encoder.
  • Add support for custom parsers. (XmlDataParser[MyParserOfData])
  • Make good and useful error messages.
  1. Where ["true", "1", "yes", "on"] is true and ["false", "0", "no", "off"] is false.

  2. Is parsed by the datetime.datetime.fromisoformat method.

  3. Is parsed as is (as string). Only check if there is data.

  4. Assume that the children can only be of type: XmlClass. Since multiple attributes are not allowed, and a data separator is not defined. 2

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

xmlclasses-0.2.0.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xmlclasses-0.2.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file xmlclasses-0.2.0.tar.gz.

File metadata

  • Download URL: xmlclasses-0.2.0.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xmlclasses-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8261ee7ad9207ac113c575951ad14491b35389f9013f968c3e8c7a8884a03a17
MD5 083661ef538d46d8db632732668264d4
BLAKE2b-256 17ee6b128c86e9353f11ed073f9eab9875243a7b07aed9b9291214bbd211caee

See more details on using hashes here.

Provenance

The following attestation bundles were made for xmlclasses-0.2.0.tar.gz:

Publisher: python-publish.yml on tacozMacleo/xmlClasses

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xmlclasses-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: xmlclasses-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xmlclasses-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a44af81568cf041475bb7e4143c9dc66eccf5182ecd4f62ade18620f80d74bdb
MD5 d19dff7df6e362396864f05fd91eb43d
BLAKE2b-256 d92410b654db2d16729419a858561090d6e7d15505b211bba65ea5c1e41b15db

See more details on using hashes here.

Provenance

The following attestation bundles were made for xmlclasses-0.2.0-py3-none-any.whl:

Publisher: python-publish.yml on tacozMacleo/xmlClasses

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page