|
2 | 2 | Usage |
3 | 3 | ===== |
4 | 4 |
|
5 | | -To use json2xml in a project:: |
| 5 | +Basic Usage |
| 6 | +----------- |
6 | 7 |
|
7 | | - import json2xml |
| 8 | +The json2xml library provides three main ways to read JSON data: |
| 9 | + |
| 10 | +* From a JSON file using ``readfromjson`` |
| 11 | +* From a URL using ``readfromurl`` |
| 12 | +* From a string using ``readfromstring`` |
| 13 | + |
| 14 | +Here's how to use each method: |
| 15 | + |
| 16 | +.. code-block:: python |
| 17 | +
|
| 18 | + from json2xml import json2xml |
| 19 | + from json2xml.utils import readfromurl, readfromstring, readfromjson |
| 20 | +
|
| 21 | + # Convert JSON data from a URL to XML |
| 22 | + data = readfromurl("https://api.example.com/data") |
| 23 | + print(json2xml.Json2xml(data).to_xml()) |
| 24 | +
|
| 25 | + # Convert a JSON string to XML |
| 26 | + data = readfromstring( |
| 27 | + '{"login":"mojombo","id":1,"avatar_url":"https://example.com/avatar.png"}' |
| 28 | + ) |
| 29 | + print(json2xml.Json2xml(data).to_xml()) |
| 30 | +
|
| 31 | + # Convert a JSON file to XML |
| 32 | + data = readfromjson("examples/licht.json") |
| 33 | + print(json2xml.Json2xml(data).to_xml()) |
| 34 | +
|
| 35 | +
|
| 36 | +Constructor Parameters |
| 37 | +---------------------- |
| 38 | + |
| 39 | +The ``Json2xml`` class accepts the following parameters: |
| 40 | + |
| 41 | +* ``data`` - The JSON data (dict or list) to convert |
| 42 | +* ``wrapper`` (default: ``"all"``) - Custom root element name |
| 43 | +* ``root`` (default: ``True``) - Whether to include the XML declaration and root element |
| 44 | +* ``pretty`` (default: ``True``) - Whether to pretty-print the XML output |
| 45 | +* ``attr_type`` (default: ``True``) - Whether to include type attributes on elements |
| 46 | +* ``item_wrap`` (default: ``True``) - Whether to wrap list items in ``<item>`` tags |
| 47 | +* ``xpath_format`` (default: ``False``) - Whether to use XPath 3.1 compliant output format |
| 48 | + |
| 49 | + |
| 50 | +Custom Wrappers and Indentation |
| 51 | +------------------------------- |
| 52 | + |
| 53 | +By default, a wrapper ``all`` and ``pretty=True`` is set. You can customize these: |
| 54 | + |
| 55 | +.. code-block:: python |
| 56 | +
|
| 57 | + from json2xml import json2xml |
| 58 | + from json2xml.utils import readfromstring |
| 59 | +
|
| 60 | + data = readfromstring( |
| 61 | + '{"login":"mojombo","id":1,"avatar_url":"https://example.com/avatar.png"}' |
| 62 | + ) |
| 63 | + print(json2xml.Json2xml(data, wrapper="all", pretty=True).to_xml()) |
| 64 | +
|
| 65 | +
|
| 66 | +Outputs: |
| 67 | + |
| 68 | +.. code-block:: xml |
| 69 | +
|
| 70 | + <?xml version="1.0" encoding="UTF-8"?> |
| 71 | + <all> |
| 72 | + <login type="str">mojombo</login> |
| 73 | + <id type="int">1</id> |
| 74 | + <avatar_url type="str">https://example.com/avatar.png</avatar_url> |
| 75 | + </all> |
| 76 | +
|
| 77 | +
|
| 78 | +Omit List Item Wrapping |
| 79 | +----------------------- |
| 80 | + |
| 81 | +By default, items in an array are wrapped in ``<item></item>`` tags. |
| 82 | + |
| 83 | +Given this JSON input: |
| 84 | + |
| 85 | +.. code-block:: json |
| 86 | +
|
| 87 | + { |
| 88 | + "my_items": [ |
| 89 | + { "my_item": { "id": 1 } }, |
| 90 | + { "my_item": { "id": 2 } } |
| 91 | + ], |
| 92 | + "my_str_items": ["a", "b"] |
| 93 | + } |
| 94 | +
|
| 95 | +Default output (``item_wrap=True``): |
| 96 | + |
| 97 | +.. code-block:: xml |
| 98 | +
|
| 99 | + <?xml version="1.0" ?> |
| 100 | + <all> |
| 101 | + <my_items type="list"> |
| 102 | + <item type="dict"> |
| 103 | + <my_item type="dict"> |
| 104 | + <id type="int">1</id> |
| 105 | + </my_item> |
| 106 | + </item> |
| 107 | + <item type="dict"> |
| 108 | + <my_item type="dict"> |
| 109 | + <id type="int">2</id> |
| 110 | + </my_item> |
| 111 | + </item> |
| 112 | + </my_items> |
| 113 | + <my_str_items type="list"> |
| 114 | + <item type="str">a</item> |
| 115 | + <item type="str">b</item> |
| 116 | + </my_str_items> |
| 117 | + </all> |
| 118 | +
|
| 119 | +To disable item wrapping: |
| 120 | + |
| 121 | +.. code-block:: python |
| 122 | +
|
| 123 | + from json2xml import json2xml |
| 124 | + from json2xml.utils import readfromstring |
| 125 | +
|
| 126 | + data = readfromstring('{"my_items":[{"my_item":{"id":1}},{"my_item":{"id":2}}],"my_str_items":["a","b"]}') |
| 127 | + print(json2xml.Json2xml(data, item_wrap=False).to_xml()) |
| 128 | +
|
| 129 | +
|
| 130 | +Output with ``item_wrap=False``: |
| 131 | + |
| 132 | +.. code-block:: xml |
| 133 | +
|
| 134 | + <?xml version="1.0" ?> |
| 135 | + <all> |
| 136 | + <my_items type="list"> |
| 137 | + <my_item type="dict"> |
| 138 | + <id type="int">1</id> |
| 139 | + </my_item> |
| 140 | + <my_item type="dict"> |
| 141 | + <id type="int">2</id> |
| 142 | + </my_item> |
| 143 | + </my_items> |
| 144 | + <my_str_items type="str">a</my_str_items> |
| 145 | + <my_str_items type="str">b</my_str_items> |
| 146 | + </all> |
| 147 | +
|
| 148 | +
|
| 149 | +Disabling Type Attributes |
| 150 | +------------------------- |
| 151 | + |
| 152 | +You can disable the type attributes on elements: |
| 153 | + |
| 154 | +.. code-block:: python |
| 155 | +
|
| 156 | + from json2xml import json2xml |
| 157 | + from json2xml.utils import readfromstring |
| 158 | +
|
| 159 | + data = readfromstring( |
| 160 | + '{"login":"mojombo","id":1,"avatar_url":"https://example.com/avatar.png"}' |
| 161 | + ) |
| 162 | + print(json2xml.Json2xml(data, wrapper="all", pretty=True, attr_type=False).to_xml()) |
| 163 | +
|
| 164 | +
|
| 165 | +Outputs: |
| 166 | + |
| 167 | +.. code-block:: xml |
| 168 | +
|
| 169 | + <?xml version="1.0" ?> |
| 170 | + <all> |
| 171 | + <login>mojombo</login> |
| 172 | + <id>1</id> |
| 173 | + <avatar_url>https://example.com/avatar.png</avatar_url> |
| 174 | + </all> |
| 175 | +
|
| 176 | +
|
| 177 | +XPath 3.1 Compliance |
| 178 | +-------------------- |
| 179 | + |
| 180 | +The library supports XPath 3.1 ``json-to-xml`` function specification from |
| 181 | +`W3C <https://www.w3.org/TR/xpath-functions-31/#func-json-to-xml>`_. |
| 182 | + |
| 183 | +When ``xpath_format=True``, the XML output uses standardized type-based element names |
| 184 | +(``map``, ``array``, ``string``, ``number``, ``boolean``, ``null``) with ``key`` attributes: |
| 185 | + |
| 186 | +.. code-block:: python |
| 187 | +
|
| 188 | + from json2xml import json2xml |
| 189 | + from json2xml.utils import readfromstring |
| 190 | +
|
| 191 | + data = readfromstring( |
| 192 | + '{"login":"mojombo","id":1,"avatar_url":"https://example.com/avatar.png"}' |
| 193 | + ) |
| 194 | + print(json2xml.Json2xml(data, xpath_format=True).to_xml()) |
| 195 | +
|
| 196 | +
|
| 197 | +Outputs: |
| 198 | + |
| 199 | +.. code-block:: xml |
| 200 | +
|
| 201 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 202 | + <map xmlns="http://www.w3.org/2005/xpath-functions"> |
| 203 | + <string key="login">mojombo</string> |
| 204 | + <number key="id">1</number> |
| 205 | + <string key="avatar_url">https://example.com/avatar.png</string> |
| 206 | + </map> |
| 207 | +
|
| 208 | +
|
| 209 | +Error Handling |
| 210 | +-------------- |
| 211 | + |
| 212 | +The library provides custom exceptions for error handling: |
| 213 | + |
| 214 | +* ``JSONReadError`` - Raised when there's an error reading a JSON file |
| 215 | +* ``URLReadError`` - Raised when there's an error fetching data from a URL |
| 216 | +* ``StringReadError`` - Raised when there's an error parsing a JSON string |
| 217 | +* ``InvalidDataError`` - Raised when the data cannot be converted to valid XML |
| 218 | + |
| 219 | +Example: |
| 220 | + |
| 221 | +.. code-block:: python |
| 222 | +
|
| 223 | + from json2xml import json2xml |
| 224 | + from json2xml.utils import readfromstring, StringReadError |
| 225 | +
|
| 226 | + try: |
| 227 | + data = readfromstring("invalid json") |
| 228 | + except StringReadError as e: |
| 229 | + print(f"Error: {e}") |
0 commit comments