Skip to main content

Module for serialize (convert) xml to Python dict (with Python objects)

Project description

XML Serializer

Allows you to convert XML to python dict (with python objects) using a schema.

Examples

You can see examples of using the module in serializer_test.py

We have next xml data (profiles.xml)

<payload>
    <MyProfile>
        <record id="1" nickname="eff1c" admin="true">
            <posts topic="something">
                <post name="test post" description="It's my test post." />
                <post name="python xml_serializer" description="It's very useful module!" />
            </posts>
        </record>
    </MyProfile>
</payload>

And we want to turn it into

{
    "payload": {
        "my_profile": {
            "record": {
                "id": 1,
                "nickname": "eff1c",
                "admin": True,
                "posts": {
                    "topic": "something",
                    "post": [
                        {"name": "test post", "description": "It's my test post."},
                        {
                            "name": "python xml_serializer",
                            "description": "It's very useful module!",
                        },
                    ],
                },
            }
        }
    }
}

We will write next schema

from xml_serializer import Tag, TagAttr
from xml_serializer.converter_types import Integer, String, Boolean

profiles_schema = {
    Tag("payload"): {
        Tag("MyProfile", "my_profile"): {
            Tag("record"): {
                TagAttr("id"): Integer(nullable=False),
                TagAttr("nickname"): String(nullable=False),
                TagAttr("admin"): Boolean(),
                Tag("posts"): {
                    TagAttr("topic"): String(nullable=False),
                    Tag("post"): [
                        {
                            TagAttr("name"): String(),
                            TagAttr("description"): String()
                        }
                    ]
                }
            }
        }
    }

Get

from xml.etree import ElementTree as etree

tree = etree.parse("profiles.xml")
root = tree.getroot()

# you can use root tag or find any else
main_tag = root.find("payload")

And call the method to pass them to

from xml_serializer import xml_serialize

response = xml_serialize(profiles_schema, main_tag)

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

xml_serializer-1.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

xml_serializer-1.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file xml_serializer-1.0.1.tar.gz.

File metadata

  • Download URL: xml_serializer-1.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.5

File hashes

Hashes for xml_serializer-1.0.1.tar.gz
Algorithm Hash digest
SHA256 616c8f55a8e2784dc53d100b3488be80de674194353d25bf7375a500d3e00d1c
MD5 17147f438da73de206321e3e7008b2f6
BLAKE2b-256 66551b6e4ae685981c94957db4d18ddd0e193983531df1324f2e9e94e27c4055

See more details on using hashes here.

File details

Details for the file xml_serializer-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: xml_serializer-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.5

File hashes

Hashes for xml_serializer-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d1bf014269d0f648b220f4a1ef95ee42465d1c80ffa9cf8531af010632a3fa8d
MD5 0071442ae8a84dbb65458bbd1eed82b0
BLAKE2b-256 4571be196ff93f366c78cee7415bdb1d0e52f9e69bead28812b368ec7c2a38ed

See more details on using hashes here.

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