Converts XML into JSON/Python dicts/arrays and vice-versa using multiple conventions
npx @tessl/cli install tessl/pypi-xmljson@0.2.0A 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.
pip install xmljsonimport xmljsonImport specific classes:
from xmljson import BadgerFish, Parker, GData, Yahoo, Abdera, CobraImport pre-configured instances:
from xmljson import badgerfish, parker, gdata, yahoo, abdera, cobrafrom 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>xmljson uses a base XMLData class that defines the core conversion framework, with specialized subclasses implementing different XML-to-JSON conventions:
etree() and data() methods with configurable optionsThe design allows for:
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): ...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): ...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): ...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): ...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): ...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): ...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): ...abdera: Abdera
badgerfish: BadgerFish
cobra: Cobra
gdata: GData
parker: Parker
yahoo: YahooThese module-level instances provide immediate access to conversion functionality without needing to instantiate classes.
__author__: str
__email__: str
__version__: strModule 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')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): ...