'Given some data, converts that data to an ``etree.Element`` suitable
for use in the XML output.'

def to_etree(self, data, options=None, name=None, depth=0):

    if isinstance(data, (list, tuple)):
        element = Element((name or u'objects'))
        if name:
            element = Element(name)
            element.set(u'type', u'list')
        else:
            element = Element(u'objects')
        for item in data:
            element.append(self.to_etree(item, options, depth=(depth + 1)))
            element[:] = sorted(element, key=(lambda x: x.tag))
    elif isinstance(data, dict):
        if (depth == 0):
            element = Element((name or u'response'))
        else:
            element = Element((name or u'object'))
            element.set(u'type', u'hash')
        for (key, value) in data.items():
            element.append(self.to_etree(value, options, name=key, depth=(depth + 1)))
            element[:] = sorted(element, key=(lambda x: x.tag))
    elif isinstance(data, Bundle):
        element = Element((name or u'object'))
        for (field_name, field_object) in data.data.items():
            element.append(self.to_etree(field_object, options, name=field_name, depth=(depth + 1)))
            element[:] = sorted(element, key=(lambda x: x.tag))
    else:
        element = Element((name or u'value'))
        simple_data = self.to_simple(data, options)
        data_type = get_type_string(simple_data)
        if (data_type != u'string'):
            element.set(u'type', get_type_string(simple_data))
        if (data_type != u'null'):
            if isinstance(simple_data, six.text_type):
                element.text = simple_data
            else:
                element.text = force_text(simple_data)
    return element
