The original library (http://github.com/quandyfactory/dict2xml/) Converts a native Python dictionary into an XML string. This is a hacky alteration to allow the library to generate invalid XML - Disgusting as this is, I needed it for a Git post-receive script
Supports item (
datetime) and collection (
dict) data types with arbitrary nesting for the collections. Datetime objects are converted to ISO format strings.
The root object passed into the
dict2xmlfunction can be any of the following data types:
To satisfy XML syntax, by default it wraps all the dict keys/elements and values in a
<root> ... </root>element. However, this can be disabled to create XML snippets.
For lists of items, if each item is also a collection data type (
dict), the elements of that item are wrapped in a generic
<item> ... </item>element.
Elements with an item data type (
unicode) include a
typeattribute with the data type. Note:
datetimedata types are converted into ISO format strings, and
datetimedata types get a
strattribute - This can be disabled thanks to my hacks
Elements with an unsupported data type raise a TypeError exception.
Download the tarballed installer -
dict2xml-[VERSION].tar.gz - for this package from the dist directory 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.
Once installed, import the library into your script and convert a dict into xml by running the
>>> import dict2xml2 >>> xml = dict2xml2.dict2xml2(some_dict)
Alternately, you can import the
dict2xml() function from the library.
>>> from dict2xml2 import dict2xml2 >>> xml = dict2xml2(some_dict)
You can also enable debugging information.
>>> import dict2xml2 >>> dict2xml2.debug = True # the console will print debug information for each function as it executes. >>> xml = dict2xml2.dict2xml2(some_object)
- Author: Ben Tasker
- Email: email@example.com
- Repository: http://github.com/bentasker/dict2xml2
Copyright and Licence
Copyright 2013 Ben Tasker
(Largely) Based on work by Ryan McGreal (c) 2012
Released under the GNU General Public Licence, Version 2: