CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-xmljson

Converts XML into JSON/Python dicts/arrays and vice-versa using multiple conventions

Overview
Eval results
Files

abdera.mddocs/

Abdera Convention

Abdera convention uses 'attributes' and 'children' keys for structured XML representation, providing explicit separation between XML attributes and child elements.

Capabilities

Abdera Class

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)
        """

Data to XML Conversion

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
    """

XML to Data Conversion

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"}

Pre-configured Instance

abdera: Abdera

A pre-configured Abdera instance available at module level for immediate use.

Convention Rules

  • Attributes: Grouped under 'attributes' key as a dictionary
  • Children: Grouped under 'children' key as an array
  • Simple Text: Elements with only text and no attributes become direct string values
  • Mixed Content: Text content mixed with child elements in children array
  • Flattening: Single child elements without attributes are flattened
  • Type Conversion: Automatic conversion of strings to booleans, integers, and floats

Structure Patterns

Complex Structure

{
  "element": {
    "attributes": {"attr1": "value1", "attr2": "value2"}, 
    "children": [
      {"child1": "text"},
      {"child2": {"attributes": {}, "children": [...]}}
    ]
  }
}

Simple Text Element

{"element": "text content"}

Element with Attributes Only

{
  "element": {
    "attributes": {"attr": "value"}
  }
}

Use Cases

  • Document Processing: When clear separation of attributes and content is needed
  • XML Parsing: Applications requiring explicit XML structure representation
  • Data Interchange: When both attributes and hierarchical data are important
  • Configuration Files: Complex XML configurations with nested structures

Advanced Example

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

Error Handling

  • Empty Elements: Elements with no content become empty dictionaries
  • Attribute Conflicts: Attributes and children are always separate
  • Unicode Handling: Proper Unicode support for attribute names and values

Install with Tessl CLI

npx tessl i tessl/pypi-xmljson@0.2.1

docs

abdera.md

badgerfish.md

cli.md

cobra.md

gdata.md

index.md

parker.md

yahoo.md

tile.json