Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/matthewandretaylor/xml-to-pydict
Parse xml to python dictionaries
https://github.com/matthewandretaylor/xml-to-pydict
dictionary parser python3 xml
Last synced: about 24 hours ago
JSON representation
Parse xml to python dictionaries
- Host: GitHub
- URL: https://github.com/matthewandretaylor/xml-to-pydict
- Owner: MatthewAndreTaylor
- License: mit
- Created: 2023-06-14T04:22:41.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-02T08:18:05.000Z (7 months ago)
- Last Synced: 2024-10-08T16:44:46.115Z (about 1 month ago)
- Topics: dictionary, parser, python3, xml
- Language: Python
- Homepage: https://pypi.org/project/xmlpydict
- Size: 43 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# xmlpydict 📑
[![XML Tests](https://github.com/MatthewAndreTaylor/xml-to-pydict/actions/workflows/tests.yml/badge.svg)](https://github.com/MatthewAndreTaylor/xml-to-pydict/actions/workflows/tests.yml)
[![PyPI versions](https://img.shields.io/badge/python-3.7%2B-blue)](https://github.com/MatthewAndreTaylor/xml-to-pydict)
[![PyPI](https://img.shields.io/pypi/v/xmlpydict.svg)](https://pypi.org/project/xmlpydict/)## Requirements
- `python 3.7+`
## Installation
To install xmlpydict, using pip:
```bash
pip install xmlpydict
```## Quickstart
```py
>>> from xmlpydict import parse
>>> parse("")
{'package': {'xmlpydict': {'@language': 'python'}}}
>>> parse("Hello!")
{'person': {'@name': 'Matthew', '#text': 'Hello!'}}
```## Goals
Create a consistent parsing strategy between XML and Python dictionaries. xmlpydict takes a more laid-back approach to enforce the syntax of XML. However, still ensures fast speeds by using finite automata.
## Features
xmlpydict allows for multiple root elements.
The root object is treated as the Python object.### xmlpydict supports the following
[CDataSection](https://www.w3.org/TR/xml/#sec-cdata-sect): CDATA Sections are stored as {'#text': CData}.
[Comments](https://www.w3.org/TR/xml/#sec-comments): Comments are tokenized for corectness, but have no effect in what is returned.
[Element Tags](https://www.w3.org/TR/xml/#sec-starttags): Allows for duplicate attributes, however only the latest defined will be taken.
[Characters](https://www.w3.org/TR/xml/#charsets): Similar to CDATA text is stored as {'#text': Char} , however this text is stripped.
### dict.get(key[, default]) will not cause exceptions
```py
# Empty tags are containers
>>> from xmlpydict import parse
>>> parse("")
{'a': {}}
>>> parse("")
{'a': {}}
>>> parse("").get('href')
None
>>> parse("")
{}
```### Attribute prefixing
```py
', attr_prefix="$")
# Change prefix from default "@" with keyword argument attr_prefix
>>> from xmlpydict import parse
>>> parse('
{"p": {"$width": "10", "$height": "5"}}
```### Exceptions
```py
# Grammar and structure of the xml_content is checked while parsing
>>> from xmlpydict import parse
>>> parse(" a>")
Exception: not well formed (violation at pos=5)
```### Unsupported
Prolog / Enforcing Document Type Definition and Element Type Declarations
Entity Referencing
Namespaces