json2xml package

Submodules

json2xml.dicttoxml module

Converts a Python dictionary or other native data type into a valid XML string.

Supports item (int, float, long, decimal.Decimal, bool, str, unicode, datetime, none and other
number-like objects) and collection (list, set, tuple and dict, as well as iterable and

dict-like objects) data types, with arbitrary nesting for the collections.

Items with a datetime type are converted to ISO format strings. Items with a None type become empty XML elements.

This module works with Python 3.7+

json2xml.dicttoxml.convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent='root')[source]

Routes the elements of an object to the right function to convert them based on their data type

json2xml.dicttoxml.convert_bool(key, val, attr_type, attr={}, cdata=False)[source]

Converts a boolean into an XML element

json2xml.dicttoxml.convert_dict(obj, ids, parent, attr_type, item_func, cdata, item_wrap)[source]

Converts a dict into an XML string.

json2xml.dicttoxml.convert_kv(key, val, attr_type, attr={}, cdata: bool = False)[source]

Converts a number or string into an XML element

json2xml.dicttoxml.convert_list(items, ids, parent, attr_type, item_func, cdata, item_wrap)[source]

Converts a list into an XML string.

json2xml.dicttoxml.convert_none(key, val, attr_type, attr={}, cdata=False)[source]

Converts a null value into an XML element

json2xml.dicttoxml.default_item_func(parent)[source]
json2xml.dicttoxml.dicttoxml(obj, root: bool = True, custom_root='root', ids=False, attr_type=True, item_wrap=True, item_func=<function default_item_func>, cdata=False)[source]

Converts a python object into XML. Arguments: - root specifies whether the output is wrapped in an XML root element

Default is True

  • custom_root allows you to specify a custom root element. Default is ‘root’

  • ids specifies whether elements get unique ids. Default is False

  • attr_type specifies whether elements get a data type attribute. Default is True

  • item_func specifies what function should generate the element name for items in a list. Default is ‘item’

  • item_wrap specifies whether to nest items in array in <item/> Default is True

  • cdata specifies whether string values should be wrapped in CDATA sections. Default is False

json2xml.dicttoxml.escape_xml(s: str) str[source]
json2xml.dicttoxml.get_unique_id(element)[source]

Returns a unique id for a given element

json2xml.dicttoxml.get_xml_type(val)[source]

Returns the data type for the xml type attribute

json2xml.dicttoxml.key_is_valid_xml(key)[source]

Checks that a key is a valid XML name

json2xml.dicttoxml.make_attrstring(attr)[source]

Returns an attribute string in the form key=”val”

json2xml.dicttoxml.make_id(element, start=100000, end=999999)[source]

Returns a random integer

json2xml.dicttoxml.make_valid_xml_name(key, attr: Dict[str, Any])[source]

Tests an XML name and fixes it if invalid

json2xml.dicttoxml.wrap_cdata(s: str) str[source]

Wraps a string into CDATA sections

json2xml.json2xml module

class json2xml.json2xml.Json2xml(data: str, wrapper: str = 'all', root: bool = True, pretty: bool = True, attr_type: bool = True, item_wrap: bool = True)[source]

Bases: object

Wrapper class to convert the data to xml

to_xml() Optional[Any][source]

Convert to xml using dicttoxml.dicttoxml and then pretty print it.

json2xml.utils module

Utils methods to convert XML data to dict from various sources

exception json2xml.utils.InvalidDataError[source]

Bases: Exception

exception json2xml.utils.JSONReadError[source]

Bases: Exception

exception json2xml.utils.StringReadError[source]

Bases: Exception

exception json2xml.utils.URLReadError[source]

Bases: Exception

json2xml.utils.readfromjson(filename: str) Dict[str, str][source]

Reads a json string and emits json string

json2xml.utils.readfromstring(jsondata: str) Dict[str, str][source]

Loads json from string

json2xml.utils.readfromurl(url: str, params: Optional[Dict[str, str]] = None) Dict[str, str][source]

Loads json from an URL over the internets

Module contents

Top-level package for json2xml.