dicttoxml 1.5.1

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

### Details

Supports item (int, float, bool, str, unicode, datetime, none) 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.

The root object passed into the dicttoxml method can be any of the supported data types.

To satisfy XML syntax, the method prepends an <?xml version=”1.0” encoding=”UTF-8” ?> element and wraps the output in a <root> … </root> element. However, this can be disabled to create XML snippets. Alternately, a custom root element can be specified by passing in the optional custom_root=foobar argument.

For lists of items, if each item is also a collection data type (lists, dict), the elements of that item are wrapped in a generic <item> … </item> element.

Each element includes an optional type attribute with the data type. By default, the type attribute it included but it can be excluded by passing an optional attr_type=False argument when calling the dicttoxml method.

Note: datetime data types are converted into ISO format strings, and unicode and datetime data types get a str attribute.

If an element name is invalid XML, it is rendered with the name “key” and the invalid name is included as a name attribute. E.g. { “^.{0,256}$”: “foo” } would be rendered <key name=”^.{0,256}$”>foo</key>. An exception is element names with spaces, which are converted to underscores.

Alternately, you can download the tarballed installer - dicttoxml-[VERSION].tar.gz - for this package from the [dist](https://github.com/quandyfactory/dicttoxml/tree/master/dist) directory on github and uncompress it. Then, from a terminal or command window, navigate into the unzipped folder and type the command:

python setup.py install

That should be all you need to do.

### Basic Usage

Once installed, import the library into your script and convert a dict into xml by running the dicttoxml function:

>>> import dicttoxml
>>> xml = dicttoxml.dicttoxml(some_dict)

Alternately, you can import the dicttoxml() function from the library.

>>> from dicttoxml import dicttoxml
>>> xml = dicttoxml(some_dict)

That’s it!

### JSON to XML

Let’s say you want to fetch a JSON object from a URL and convert it into XML. Here’s how you can do that:

As you can see, the only difference is that the type attributes are now absent.

### Custom Root

By default, dicttoxml wraps all the elements in a <root> … </root> element. Starting in version 1.5, you can change the name of the root element to something else by passing an optional custom_root=some_custom_root argument to the dicttoxml method.

This makes the XML easier to read. If it is not well-formed, the xml parser will raise an exception.

### Unique ID Attributes

Starting in version 1.1, you can set an optional ids parameter so that dicttoxml gives each element a unique id attribute.

With the ids flag on, the function generates a unique randomly-generated ID for each element based on the parent element in the form parent_unique. For list items, the id is in the form parent_unique_index.

Updated convert_kv(), convert_bool() and convert_none() functions to test whether the key is a valid XML name and, if it is not, to render it as <key name=”{invalidname}”>value</key>. This addresses [issue 10](https://github.com/quandyfactory/dicttoxml/issues/10).