The xml.marshal package contains code for marshalling Python data types and objects into XML. The xml.marshal.generic module uses a simple DTD of its own, and provides Marshaller and Unmarshaller classes that can be subclassed to marshal objects using a different DTD. As an example, xml.marshal.wddx marshals Python objects into the WDDX DTD.
The interface is the same as the standard Python marshal module: dump(value, file) and dumps(value) convert value into XML and either write it to the given file or return it as a string, while load(file) and loads(string) perform the reverse conversion. For example:
>>> generic.dumps( (1, 2.0, 'name', [2,3,5,7]) ) """<?xml version="1.0"?> <marshal> <tuple> <int>1</int> <float>2.0</float> <string>name</string> <list id="i2"> <int>2</int> <int>3</int> <int>5</int> <int>7</int> </list> </tuple> </marshal>""" >>>
(The output has been pretty-printed for clarity.)
Note that, at least in the generic module, strings are simply incorporated in the XML output and therefore can't contain control characters that are illegal in XML. If you need to marshal such strings, you'll have to encode them using the binascii module before calling the dump() function.