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

parker.mddocs/

Parker Convention

Parker convention produces the most compact JSON representation by using tail nodes for text content and ignoring XML attributes entirely. It's ideal when attributes aren't needed and minimal JSON output is desired.

Capabilities

Parker Class

Creates a Parker converter that ignores attributes and produces compact JSON.

class Parker(XMLData):
    def __init__(self, **kwargs):
        """
        Initialize Parker 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 Parker 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 Parker convention with optional root preservation.

def data(self, root, preserve_root=False):
    """
    Convert etree.Element into a dictionary.
    
    Parameters:
    - root: Element, XML element to convert
    - preserve_root: bool, whether to preserve root element (default: False)
    
    Returns:
    dict or primitive: Dictionary representation using Parker convention
    """

Usage Examples:

from xmljson import parker
from xml.etree.ElementTree import fromstring
import json

# Basic conversion (root absorbed)
xml_string = '<x><a>1</a><b>2</b></x>'
xml_element = fromstring(xml_string)
data = parker.data(xml_element)
print(json.dumps(data))
# Output: {"a": 1, "b": 2}

# Preserve root element
data = parker.data(xml_element, preserve_root=True)
print(json.dumps(data))
# Output: {"x": {"a": 1, "b": 2}}

# Simple text elements
xml_string = '<item>Hello World</item>'
xml_element = fromstring(xml_string)
data = parker.data(xml_element)
print(data)
# Output: "Hello World"

Pre-configured Instance

parker: Parker

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

Convention Rules

  • Attributes: Completely ignored and not included in output
  • Root Element: Absorbed by default (can be preserved with preserve_root=True)
  • Text Content: Becomes the direct value (no special key)
  • Empty Elements: Become empty strings
  • Arrays: Multiple elements with same name become arrays
  • Type Conversion: Automatic conversion of strings to booleans, integers, and floats
  • Whitespace: Text content is automatically trimmed

Use Cases

  • Configuration Files: When XML attributes aren't needed
  • Data Exchange: Minimal JSON payload size required
  • Simple XML Structures: Converting basic XML data to JSON
  • API Responses: When compact JSON representation is preferred

Limitations

  • No Attribute Support: XML attributes are completely lost
  • No Namespace Support: XML namespaces are not handled
  • Root Information: Root element name lost unless preserve_root=True

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