Recursively convert the data into xml.

        This function was originally copied from the
        `Piston project <https://bitbucket.org/jespern/django-piston/>`_
        It has been modified since.

        :param xml: the xml document
        :type xml: SimplerXMLGenerator
        :param data: data to be formatted
        :param key: name of the parent element (for root this is ``None``)

def _to_xml(self, xml, data, key=None):
        """ Recursively convert the data into xml.

        This function was originally copied from the
        `Piston project <https://bitbucket.org/jespern/django-piston/>`_
        It has been modified since.

        :param xml: the xml document
        :type xml: SimplerXMLGenerator
        :param data: data to be formatted
        :param key: name of the parent element (for root this is ``None``)
        """

        if isinstance(data, (list, tuple)):
            for item in data:
                elemname = self._list_item_element_name(key)
                xml.startElement(elemname, {})
                self._to_xml(xml, item)
                xml.endElement(elemname)
        elif isinstance(data, dict):
            for key, value in data.iteritems():
                xml.startElement(key, {})
                self._to_xml(xml, value, key)
                xml.endElement(key)
        else:
            xml.characters(smart_unicode(data))