json2xml package

Submodules

json2xml.dicttoxml module

json2xml.dicttoxml.convert(obj: ELEMENT, ids: Any, attr_type: bool, item_func: Callable[[str], str], cdata: bool, item_wrap: bool, parent: str = 'root', list_headers: bool = False) str[source]

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

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

Converts a boolean into an XML element

json2xml.dicttoxml.convert_dict(obj: dict[str, Any], ids: list[str], parent: str, attr_type: bool, item_func: Callable[[str], str], cdata: bool, item_wrap: bool, list_headers: bool = False) str[source]

Converts a dict into an XML string.

json2xml.dicttoxml.convert_kv(key: str, val: str | numbers.Number, attr_type: bool, attr: dict[str, Any] = {}, cdata: bool = False) str[source]

Converts a number or string into an XML element

json2xml.dicttoxml.convert_list(items: Sequence[Any], ids: list[str], parent: str, attr_type: bool, item_func: Callable[[str], str], cdata: bool, item_wrap: bool, list_headers: bool = False) str[source]

Converts a list into an XML string.

json2xml.dicttoxml.convert_none(key: str, attr_type: bool, attr: dict[str, Any] = {}, cdata: bool = False) str[source]

Converts a null value into an XML element

json2xml.dicttoxml.default_item_func(parent: str) str[source]
json2xml.dicttoxml.dict2xml_str(attr_type: bool, attr: dict[str, Any], item: dict[str, Any], item_func: Callable[[str], str], cdata: bool, item_name: str, item_wrap: bool, parentIsList: bool, parent: str = '', list_headers: bool = False) str[source]

parse dict2xml

json2xml.dicttoxml.dicttoxml(obj: dict[str, Any], root: bool = True, custom_root: str = 'root', ids: list[int] | None = None, attr_type: bool = True, item_wrap: bool = True, item_func: Callable[[str], str] = <function default_item_func>, cdata: bool = False, xml_namespaces: dict[str, Any] = {}, list_headers: bool = False) bytes[source]

Converts a python object into XML.

Parameters
  • obj (dict) – dictionary

  • root (bool) – Default is True specifies wheter the output is wrapped in an XML root element

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

  • ids (bool) – Default is False specifies whether elements get unique ids.

  • attr_type (bool) – Default is True specifies whether elements get a data type attribute.

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

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

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

  • xml_namespaces

    is a dictionary where key is xmlns prefix and value the urn, Default is {}. Example:

    { 'flex': 'http://www.w3.org/flex/flexBase', 'xsl': "http://www.w3.org/1999/XSL/Transform"}
    

    results in

    <root xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:flex="http://www.w3.org/flex/flexBase">
    

  • list_headers (bool) –

    Repeats the header for every element in a list. Example if True:

    "Bike": [
    {'frame_color': 'red'},
    {'frame_color': 'green'}
    ]}
    

    results in

    <Bike><frame_color>red</frame_color></Bike>
    <Bike><frame_color>green</frame_color></Bike>
    

Dictionaries-keys with special char ‘@’ has special meaning: @attrs: This allows custom xml attributes:

{'@attr':{'a':'b'}, 'x':'y'}

results in

<root a="b"><x>y</x></root>

@flat: If a key ends with @flat (or dict contains key @flat’), encapsulating node is omitted. Similar to item_wrap. @val: @attrs requires complex dict type. If primitive type should be used, then @val is used as key. To add custom xml-attributes on a list {‘list’: [4, 5, 6]}, you do this:

{'list': {'@attrs': {'a':'b','c':'d'}, '@val': [4, 5, 6]}

which results in

<list a="b" c="d"><item>4</item><item>5</item><item>6</item></list>
json2xml.dicttoxml.escape_xml(s: str | numbers.Number) str[source]
json2xml.dicttoxml.get_unique_id(element: str) str[source]

Returns a unique id for a given element

json2xml.dicttoxml.get_xml_type(val: Union[str, int, float, bool, numbers.Number, collections.abc.Sequence, datetime.datetime, datetime.date, None, Dict[str, Any]]) str[source]

Returns the data type for the xml type attribute

json2xml.dicttoxml.is_primitive_type(val: Any) bool[source]
json2xml.dicttoxml.key_is_valid_xml(key: str) bool[source]

Checks that a key is a valid XML name

json2xml.dicttoxml.list2xml_str(attr_type: bool, attr: dict[str, Any], item: Sequence[Any], item_func: Callable[[str], str], cdata: bool, item_name: str, item_wrap: bool, list_headers: bool = False) str[source]
json2xml.dicttoxml.make_attrstring(attr: dict[str, Any]) str[source]

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

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

Returns a random integer

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

Tests an XML name and fixes it if invalid

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

Wraps a string into CDATA sections

json2xml.json2xml module

class json2xml.json2xml.Json2xml(data: dict[str, Any], 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() Any | None[source]

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

json2xml.utils module

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: dict[str, str] | None = None) dict[str, str][source]

Loads json from an URL over the internets

Module contents

Top-level package for json2xml.