CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-xmljson

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

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

xmljson

A Python library that converts XML into JSON/Python dictionaries and vice-versa, supporting 6 different XML-to-JSON conventions including BadgerFish, Abdera, Cobra, GData, Parker, and Yahoo. It handles bidirectional conversion between XML structures and Python data structures with customizable parsing behaviors.

Package Information

  • Package Name: xmljson
  • Language: Python
  • Installation: pip install xmljson

Core Imports

import xmljson

Import specific classes:

from xmljson import BadgerFish, Parker, GData, Yahoo, Abdera, Cobra

Import pre-configured instances:

from xmljson import badgerfish, parker, gdata, yahoo, abdera, cobra

Basic Usage

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

# XML to data conversion
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"}}}

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

Architecture

xmljson uses a base XMLData class that defines the core conversion framework, with specialized subclasses implementing different XML-to-JSON conventions:

  • XMLData: Base class providing etree() and data() methods with configurable options
  • Convention Classes: Specialized implementations (BadgerFish, Parker, etc.) with preset configurations
  • Pre-configured Instances: Module-level instances for convenience

The design allows for:

  • Customizable string parsing and type conversion
  • Flexible dictionary and list constructors (OrderedDict, custom types)
  • Configurable handling of XML attributes, text content, and invalid tags
  • Support for both xml.etree and lxml backends

Capabilities

BadgerFish Convention

XML conversion using BadgerFish convention with @ prefix for attributes and $ for text content. This convention is widely supported and provides a clean JSON representation of XML structures.

class BadgerFish(XMLData):
    def __init__(self, **kwargs): ...

def etree(self, data, root=None): ...
def data(self, root): ...

BadgerFish Convention

Parker Convention

XML conversion using Parker convention with tail nodes for text content and ignoring attributes. Produces the most compact JSON representation by omitting attribute information.

class Parker(XMLData):
    def __init__(self, **kwargs): ...

def etree(self, data, root=None): ...
def data(self, root, preserve_root=False): ...

Parker Convention

GData Convention

XML conversion using GData convention with $t for text content and attributes added as-is. This convention is used by Google's GData APIs.

class GData(XMLData):
    def __init__(self, **kwargs): ...

def etree(self, data, root=None): ...
def data(self, root): ...

GData Convention

Yahoo Convention

XML conversion using Yahoo convention with 'content' for text and no automatic string parsing. Values remain as strings unless explicitly converted.

class Yahoo(XMLData):
    def __init__(self, **kwargs): ...

def etree(self, data, root=None): ...
def data(self, root): ...

Yahoo Convention

Abdera Convention

XML conversion using Abdera convention with 'attributes' and 'children' keys for structured XML representation.

class Abdera(XMLData):
    def __init__(self, **kwargs): ...

def etree(self, data, root=None): ...
def data(self, root): ...

Abdera Convention

Cobra Convention

XML conversion using Cobra convention with sorted attributes and string-only values. This convention ensures consistent attribute ordering and string type preservation.

class Cobra(XMLData):
    def __init__(self, **kwargs): ...

def etree(self, data, root=None): ...
def data(self, root): ...

Cobra Convention

Command Line Interface

XML to JSON conversion via command line tool supporting all conventions with customizable input/output files.

def main(*test_args): ...
def parse_args(args=None, in_file=sys.stdin, out_file=sys.stdout): ...

Command Line Interface

Pre-configured Instances

abdera: Abdera
badgerfish: BadgerFish  
cobra: Cobra
gdata: GData
parker: Parker
yahoo: Yahoo

These module-level instances provide immediate access to conversion functionality without needing to instantiate classes.

Module Constants

__author__: str
__email__: str  
__version__: str

Module metadata constants providing package information:

  • __author__: Package author name ('S Anand')
  • __email__: Author email address ('root.node@gmail.com')
  • __version__: Current package version ('0.2.0')

Types

class XMLData:
    def __init__(
        self,
        xml_fromstring=True,
        xml_tostring=True, 
        element=None,
        dict_type=None,
        list_type=None,
        attr_prefix=None,
        text_content=None,
        simple_text=False,
        invalid_tags=None
    ): ...
    
    def etree(self, data, root=None): ...
    def data(self, root): ...
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/xmljson@0.2.x
Publish Source
CLI
Badge
tessl/pypi-xmljson badge