Skip to main content

XML JSON conversion tool

Project description

XmlDict-light - Lightweight XML Library

Description: XmlDict is a lightweight Python library for working with XML data, designed to be dependency-free and utilizing only standard Python libraries (specifically xml.sax.saxutils and xml.dom.minidom). It provides basic XML node manipulation, attribute handling, and conversion between XML and dictionary representations. It's a simplified version of a full-fledged XML parser, focusing on core conversion tasks.

Key Features:

  • Node-Based Structure: Represents XML data as a tree of nodes.
  • Attribute Handling: Allows setting, getting, and deleting node attributes.
  • Text Content: Supports storing text content within nodes.
  • Conversion to Dictionary: Provides methods for converting XML data into a Python dictionary and vice versa.
  • Dependency-Free: Does not require any external libraries beyond Python's standard library.

Usage examples

from xmldict_light import XmlDict

# create xml from dict
xml_dict = XmlDict.from_dict({"root": {"one": 1, "two": 2}})
xml_dict.three = 3
print(xml_dict.to_xml())

# create dict from xml
xml_dict = XmlDict.from_xml("<root><one>1</one><two>2</two></root>")
xml_dict.three = 3
print(xml_dict.to_dict())

Classes and Methods:

  • XmlDict:
    • The main class for representing and manipulating XML data.
    • __init__(self, name: str): Initializes the root node of the XML document.
    • add_node(self, name: str) -> Node: Adds a new node as a child of the root node.
    • to_dict(self) -> dict: Converts the entire XML structure to a dictionary.
    • to_xml(self, xml_dec: bool = True) -> str: Converts the entire XML structure to an XML string.
    • root_node(self) -> Node: Returns the root node of the XML document.
    • from_dict(cls, dict_obj: dict) -> Self: Creates an XmlDict object from a Python dictionary.
    • from_xml(cls, xml_str: str) -> Self: Creates an XmlDict object from an XML string.
  • XmlDict.Node:
    • Represents an individual XML node in the tree structure.
    • __init__(self, name: str): Initializes a new node with the given tag name.
    • add_node(self, name: str) -> Node: Adds a child node to the current node.
    • attributes(self) -> list[str | int | None]: Returns a list of attributes associated with the node.
    • get_attribute(self, name: str) -> str | int | None: Retrieves a specific attribute value.
    • set_attribute(self, name: str, value: str | int | None): Sets the value of an attribute.
    • delete_attribute(self, name: str): Removes an attribute from the node.
    • name(self) -> str: Returns the tag name of the node.
    • text(self) -> str: Returns the text content of the node.
    • parent(self) -> Self | None: Returns the parent node of the current node.
    • children(self) -> list[Self]: Returns a list of child nodes.
    • to_dict(self) -> dict: Converts the node and its children to a dictionary representation.
    • to_xml(self, xml_dec: bool = True) -> str: Converts the node and its children to an XML string representation.

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

xmldict_light-1.1.6.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

xmldict_light-1.1.6-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file xmldict_light-1.1.6.tar.gz.

File metadata

  • Download URL: xmldict_light-1.1.6.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for xmldict_light-1.1.6.tar.gz
Algorithm Hash digest
SHA256 a3b10fbbf8babd4d581cc10acc948e0635b71a1fd570738c1e228b15f898ce4a
MD5 b08a439e79132c9e4cd3b16339218f54
BLAKE2b-256 9d71cee4047cf1f003b26ce5d04513f8b2eed766a7cae83169bf16a574ca0e26

See more details on using hashes here.

File details

Details for the file xmldict_light-1.1.6-py3-none-any.whl.

File metadata

  • Download URL: xmldict_light-1.1.6-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for xmldict_light-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 abb0ad28701d12818039f45671e6a5b10ea4e0d7cbc783fe703e5e2a28578b70
MD5 8fc1f0566abb0a1c160a03518fa498a1
BLAKE2b-256 d6b35d9a789d53bbc6f8841f5d42140b6a534c01a2bb4c3f381850397d0d364b

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