xmljson

0.2.1 · abandoned · verified Sat Apr 11

xmljson is a Python library that converts XML into JSON/Python dictionaries/arrays and vice-versa. It offers various conversion conventions like BadgerFish, Abdera, Cobra, GData, Parker, and Yahoo to manage the mapping of XML structures to JSON. The library's current version is 0.2.1, but it is explicitly marked as not actively maintained and its GitHub repository is archived, indicating an abandoned status.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates converting an XML string to a Python dictionary (which can then be serialized to JSON) and converting the dictionary back to an XML ElementTree object, using the BadgerFish convention. It highlights how attributes (e.g., 'value') are prefixed with '@' and text content with '$' in the resulting dictionary structure.

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

# Example XML string
xml_string = '<employees><person><name value="Alice"/></person><person><name value="Bob"/></person></employees>'

# Convert XML to a Python dictionary using BadgerFish convention
xml_element = fromstring(xml_string)
data_dict = bf.data(xml_element)
print('Python dict (BadgerFish):')
print(json.dumps(data_dict, indent=2))

# Convert Python dictionary back to XML
xml_from_dict = bf.etree(data_dict)
print('\nXML from dict (BadgerFish):')
print(tostring(xml_from_dict[0]).decode())

view raw JSON →