Converts XML into JSON/Python dicts/arrays and vice-versa using multiple conventions
Abdera convention uses 'attributes' and 'children' keys for structured XML representation, providing explicit separation between XML attributes and child elements.
Creates an Abdera converter with explicit attributes and children structure.
class Abdera(XMLData):
def __init__(self, **kwargs):
"""
Initialize Abdera converter.
Parameters:
- **kwargs: Additional XMLData parameters (xml_fromstring, xml_tostring,
element, dict_type, list_type, simple_text, invalid_tags)
"""Converts Python dictionaries and lists to XML elements using Abdera convention.
def etree(self, data, root=None):
"""
Convert data structure into a list of etree.Element.
Parameters:
- data: dict or list, data structure to convert
- root: Element, optional root element to append to
Returns:
List of etree.Element objects or modified root element
"""Converts XML elements to Python dictionaries using Abdera convention with explicit 'attributes' and 'children' structure.
def data(self, root):
"""
Convert etree.Element into a dictionary using Abdera convention.
Abdera convention uses explicit 'attributes' and 'children' keys:
- 'attributes': Dictionary containing all XML attributes
- 'children': List containing child elements and text content
- Single child elements without attributes are flattened
- Text-only elements become direct string values
Parameters:
- root: Element, XML element to convert
Returns:
dict: Dictionary with element name as key and structured value:
- Simple text: {"element": "text"}
- Complex: {"element": {"attributes": {...}, "children": [...]}}
"""Usage Examples:
from xmljson import abdera
from xml.etree.ElementTree import fromstring, tostring
import json
# XML to data conversion
xml_string = '<book id="123" lang="en"><title>Python Guide</title><author>John Doe</author></book>'
xml_element = fromstring(xml_string)
data = abdera.data(xml_element)
print(json.dumps(data, indent=2))
# Output:
# {
# "book": {
# "attributes": {
# "id": "123",
# "lang": "en"
# },
# "children": [
# {
# "title": "Python Guide"
# },
# {
# "author": "John Doe"
# }
# ]
# }
# }
# Simple text element (flattened)
xml_simple = '<title>Just Text</title>'
data_simple = abdera.data(fromstring(xml_simple))
print(json.dumps(data_simple))
# Output: {"title": "Just Text"}abdera: AbderaA pre-configured Abdera instance available at module level for immediate use.
{
"element": {
"attributes": {"attr1": "value1", "attr2": "value2"},
"children": [
{"child1": "text"},
{"child2": {"attributes": {}, "children": [...]}}
]
}
}{"element": "text content"}{
"element": {
"attributes": {"attr": "value"}
}
}from xmljson import abdera
from xml.etree.ElementTree import fromstring
import json
# Complex XML with mixed content
xml_complex = '''
<document version="1.0" encoding="utf-8">
This is document text
<section id="intro">
<title>Introduction</title>
<paragraph>First paragraph</paragraph>
<paragraph>Second paragraph</paragraph>
</section>
<section id="content">
<title>Main Content</title>
More text here
</section>
</document>
'''
xml_element = fromstring(xml_complex)
data = abdera.data(xml_element)
print(json.dumps(data, indent=2))
# Shows clear separation between attributes, text content, and child elements