Converts XML into JSON/Python dicts/arrays and vice-versa using multiple conventions
Yahoo convention uses 'content' for text content and disables automatic string parsing, keeping all values as strings unless explicitly converted. It follows the format used by Yahoo's JSON APIs.
Creates a Yahoo converter with 'content' for text and string preservation.
class Yahoo(XMLData):
def __init__(self, **kwargs):
"""
Initialize Yahoo converter.
Parameters:
- **kwargs: Additional XMLData parameters (xml_fromstring, xml_tostring,
element, dict_type, list_type, simple_text, invalid_tags)
Note: xml_fromstring defaults to False (no automatic type conversion)
"""Converts Python dictionaries and lists to XML elements using Yahoo 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
"""Converts XML elements to Python dictionaries using Yahoo convention.
def data(self, root):
"""
Convert etree.Element into a dictionary.
Parameters:
- root: Element, XML element to convert
Returns:
dict: Dictionary representation using Yahoo convention
"""Usage Examples:
from xmljson import yahoo
from xml.etree.ElementTree import fromstring, tostring
import json
# XML to data conversion (strings preserved)
xml_string = '<item id="123"><title>Sample</title><count>42</count><active>true</active></item>'
xml_element = fromstring(xml_string)
data = yahoo.data(xml_element)
print(json.dumps(data))
# Output: {"item": {"id": "123", "title": "Sample", "count": "42", "active": "true"}}
# Data to XML conversion
data = {
'item': {
'id': '123',
'title': 'Sample Title',
'content': 'This is the content'
}
}
elements = yahoo.etree(data)
print(tostring(elements[0]).decode())
# Output: <item id="123"><title>Sample Title</title>This is the content</item>yahoo: YahooA pre-configured Yahoo instance available at module level for immediate use.
The Yahoo convention's key feature is string preservation:
from xmljson import yahoo, badgerfish
from xml.etree.ElementTree import fromstring
import json
xml_string = '<data><number>42</number><flag>true</flag><text>hello</text></data>'
xml_element = fromstring(xml_string)
# Yahoo preserves strings
yahoo_data = yahoo.data(xml_element)
print("Yahoo:", json.dumps(yahoo_data))
# Output: Yahoo: {"data": {"number": "42", "flag": "true", "text": "hello"}}
# BadgerFish converts types
bf_data = badgerfish.data(xml_element)
print("BadgerFish:", json.dumps(bf_data))
# Output: BadgerFish: {"data": {"number": {"$": 42}, "flag": {"$": true}, "text": {"$": "hello"}}}