https://github.com/gotthardp/python-xml2dict
Flexible XML to dict Converter
https://github.com/gotthardp/python-xml2dict
parser python xml xml-parser
Last synced: 5 months ago
JSON representation
Flexible XML to dict Converter
- Host: GitHub
- URL: https://github.com/gotthardp/python-xml2dict
- Owner: gotthardp
- License: mit
- Created: 2019-12-08T19:23:59.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-20T17:54:23.000Z (about 6 years ago)
- Last Synced: 2024-04-29T14:43:24.689Z (almost 2 years ago)
- Topics: parser, python, xml, xml-parser
- Language: Python
- Homepage:
- Size: 6.84 KB
- Stars: 10
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Flexible XML to dict Converter
```bash
pip install python-xml2dict
```
```python
import xml2dict
```
## Default Behaviour
The `parse` function takes a string or a file as a parameter. All other
parameters are optional.
```python
xml = '''
A
B
C
D
1.1.2019
'''
print(json.dumps(xml2dict.parse(xml), indent=4))
```
```python
{
"list": {
"items": {
"item": [
{
"@id": "1",
"#text": "A"
},
{
"@id": "2",
"#text": "B"
},
{
"@id": "3",
"#text": "C"
},
{
"@id": "4",
"#text": "D"
}
]
},
"date": "1.1.2019"
}
}
```
## Customized Behaviour
Behaviour can be modified by defining custom processing functions, which may be
applicable to one, more or all XML elements.
### Put items with unique keys to a dict
```python
def get_key(obj, name, val, arg):
if name == 'id':
arg['key'] = val
else:
obj[tag] = val
def add_item(obj, tag, val, arg):
if tag not in obj:
obj[tag] = {arg['key']: val}
else:
obj[tag][arg['key']] = val
schema = [
{"elem": ["item"], "attr": get_key, "add": add_item}
]
print(json.dumps(xml2dict.parse(xml, schema), indent=4))
```
```python
{
"list": {
"items": {
"item": {
"1": "A",
"2": "B",
"3": "C",
"4": "D"
}
},
"date": "1.1.2019"
}
}
```
### Use different processing for certain items
If the processing function does not store the value into a dict, only the
remaining items will be returned.
```python
def print_item(obj, tag, val, arg):
print("ITEM", tag, val)
schema = [
{"elem": ["item"], "add": print_item}
]
print(json.dumps(xml2dict.parse(xml, schema), indent=4))
```
```python
ITEM item {'@id': '1', '#text': 'A'}
ITEM item {'@id': '2', '#text': 'B'}
ITEM item {'@id': '3', '#text': 'C'}
ITEM item {'@id': '4', '#text': 'D'}
{
"list": {
"items": null,
"date": "1.1.2019"
}
}
```