json2xml package

Submodules

json2xml.dicttoxml module

json2xml.dicttoxml.convert(obj: str | int | float | bool | Number | Sequence[str] | datetime | date | None | dict[str, Any], 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 | 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] | None, 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, ~typing.Any], root: bool = True, custom_root: str = 'root', ids: list[int] | None = None, attr_type: bool = True, item_wrap: bool = True, item_func: ~collections.abc.Callable[[str], str] = <function default_item_func>, cdata: bool = False, xml_namespaces: dict[str, ~typing.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/> Example if True

    ..code-block:: python

    data = {‘bike’: [‘blue’, ‘green’]}

    <bike>
    <item>blue</item>
    <item>green</item>
    </bike>
    

    Example if False

    ..code-block:: python

    data = {‘bike’: [‘blue’, ‘green’]}

    ..code-block:: xml

    <bike>blue</bike> <bike>green</bike>’

  • 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) –

    Default is False 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 | Number) str[source]

Escape a string for use in XML.

Args:

s (str | numbers.Number): The string to escape.

Returns:

str: The escaped string.

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

Generate a unique ID for a given element.

Args:

element (str): The element to generate an ID for.

Returns:

str: The unique ID.

json2xml.dicttoxml.get_xml_type(val: str | int | float | bool | Number | Sequence[str] | datetime | date | None | dict[str, Any]) str[source]

Get the XML type of a given value.

Args:

val (ELEMENT): The value to get the type of.

Returns:

str: The XML type.

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

Check if a key is a valid XML name.

Args:

key (str): The key to check.

Returns:

bool: True if the key is a valid XML name, False otherwise.

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]

Create a string of XML attributes from a dictionary.

Args:

attr (dict[str, Any]): The dictionary of attributes.

Returns:

str: The string of XML attributes.

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

Generate a random ID for a given element.

Args:

element (str): The element to generate an ID for. start (int, optional): The lower bound for the random number. Defaults to 100000. end (int, optional): The upper bound for the random number. Defaults to 999999.

Returns:

str: The generated ID.

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 | Number) str[source]

Wraps a string into CDATA sections

json2xml.json2xml module

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

Utility methods for converting XML data to dictionary from various sources.

exception json2xml.utils.InvalidDataError[source]

Bases: Exception

Raised when the data is invalid.

exception json2xml.utils.JSONReadError[source]

Bases: Exception

Raised when there is an error reading JSON data.

exception json2xml.utils.StringReadError[source]

Bases: Exception

Raised when there is an error reading from a string.

exception json2xml.utils.URLReadError[source]

Bases: Exception

Raised when there is an error reading from a URL.

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

Reads a JSON file and returns a dictionary.

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

Loads JSON data from a string and returns a dictionary.

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

Loads JSON data from a URL and returns a dictionary.

Module contents

Top-level package for json2xml.