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 etree element (tag)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xml_serializer-1.0.2.tar.gz.
File metadata
- Download URL: xml_serializer-1.0.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30f71ca33d16d389893a8a05827669b296f912011acd875ff05612431b73e425
|
|
| MD5 |
fccfa1339d9b8f1459210df9bdef0a2a
|
|
| BLAKE2b-256 |
73227c85279fadc4c722ad58f0dabf494f4919c05ba2683b350e202d279021f3
|
File details
Details for the file xml_serializer-1.0.2-py3-none-any.whl.
File metadata
- Download URL: xml_serializer-1.0.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9cd1801d8f5ac85e8a1bd003f7fa2f56738c82003cf844b475853da54e2bc960
|
|
| MD5 |
c301499c70b0e51f16ba4709359c5396
|
|
| BLAKE2b-256 |
b5d32ad7c47a9cf6d20ee9e862b8a3b5b4dfd6a8ddeec3c33e274edcbf980564
|