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

badgerfish.mddocs/

BadgerFish Convention

BadgerFish convention uses @ prefix for XML attributes and $ for text content, creating a clean and widely-supported JSON representation of XML structures.

Capabilities

BadgerFish Class

Creates a BadgerFish converter with @ prefix for attributes and $ for text content.

class BadgerFish(XMLData):
    def __init__(self, **kwargs):
        """
        Initialize BadgerFish 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 BadgerFish 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
    """

Usage Example:

from xmljson import badgerfish as bf
from xml.etree.ElementTree import Element, tostring

# Convert dictionary to XML
data = {'p': {'@id': 'main', '$': 'Hello', 'b': 'bold'}}
elements = bf.etree(data)
print(tostring(elements[0]).decode())
# Output: <p id="main">Hello<b>bold</b></p>

# Convert with existing root element
root = Element('root')
result = bf.etree({'p': {'@id': 'main'}}, root=root)
print(tostring(result).decode())
# Output: <root><p id="main"/></root>

XML to Data Conversion

Converts XML elements to Python dictionaries using BadgerFish convention.

def data(self, root):
    """
    Convert etree.Element into a dictionary.
    
    Parameters:
    - root: Element, XML element to convert
    
    Returns:
    dict: Dictionary representation using BadgerFish convention
    """

Usage Example:

from xmljson import badgerfish as bf
from xml.etree.ElementTree import fromstring
import json

# Convert XML to dictionary
xml_string = '<p id="main">Hello<b>bold</b></p>'
xml_element = fromstring(xml_string)
data = bf.data(xml_element) 
print(json.dumps(data))
# Output: {"p": {"@id": "main", "$": "Hello", "b": {"$": "bold"}}}

Pre-configured Instance

badgerfish: BadgerFish

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

Convention Rules

  • Attributes: Prefixed with @ (e.g., id="main" becomes "@id": "main")
  • Text Content: Stored in $ key (e.g., <p>text</p> becomes {"p": {"$": "text"}})
  • Elements: Element names become dictionary keys
  • Arrays: Multiple elements with same name become arrays
  • Type Conversion: Automatic conversion of strings to booleans, integers, and floats
  • Boolean Values: true/false (lowercase) in XML, True/False in Python

Error Handling

  • Invalid XML Tags: Can be dropped using invalid_tags='drop' parameter
  • Type Conversion Errors: Falls back to string representation
  • Namespace Handling: Not yet supported (raises ValueError)

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