'Export the resource as element tree
@param start: index of the first record to export
@param limit: maximum number of records to export
@param msince: minimum modification date of the records
@param fields: data fields to include (default: all)
@param references: foreign keys to include (default: all)
@param dereference: also export referenced records
@param maxdepth:
@param mcomponents: components of the master resource to
include (list of tablenames), empty list
for all
@param rcomponents: components of referenced resources to
include (list of tablenames), empty list
for all
@param filters: additional URL filters (Sync), as dict
{tablename: {url_var: string}}
@param maxbounds: include lat/lon boundaries in the top
level element (off by default)
@param xmlformat:
@param location_data: dictionary of location data which has been
looked-up in bulk ready for xml.gis_encode()
@param target: alias of component targetted (or None to target master resource)
@param map_data: dictionary of options which can be read by the map'

def export_tree(self, start=0, limit=None, msince=None, fields=None, references=None, dereference=True, maxdepth=MAXDEPTH, mcomponents=None, rcomponents=None, filters=None, maxbounds=False, xmlformat=None, location_data=None, map_data=None, target=None):

    xml = current.xml
    if xml.show_urls:
        base_url = current.response.s3.base_url
    else:
        base_url = None
    self.muntil = None
    self.results = 0
    lazy = []
    current.auth_user_represent = S3Represent(lookup='auth_user', fields=['email'])
    table = self.table
    if (xml.filter_mci and ('mci' in table.fields)):
        mci_filter = (table.mci >= 0)
        self.add_filter(mci_filter)
    tablename = self.tablename
    if (filters and (tablename in filters)):
        queries = S3URLQuery.parse(self, filters[tablename])
        add_filter = self.add_filter
        [add_filter(q) for a in queries for q in queries[a]]
    if ((msince is not None) and ('modified_on' in table.fields)):
        orderby = ('%s    ASC' % table['modified_on'])
    else:
        orderby = None
    components = self.components_to_export(self.tablename, mcomponents)
    prefix = self.prefix
    name = self.name
    if base_url:
        url = ('%s/%s/%s' % (base_url, prefix, name))
    else:
        url = ('/%s/%s' % (prefix, name))
    (rfields, dfields) = self.split_fields(data=fields, references=references)
    if xmlformat:
        (include, exclude) = xmlformat.get_fields((target or tablename))
    else:
        (include, exclude) = (None, None)
    self.load(fields=include, skip=exclude, start=start, limit=limit, orderby=orderby, virtual=False, cacheable=True)
    results = self.count()
    if ((not target) and (not location_data)):
        location_data = (current.gis.get_location_data(self, count=results) or {})
    root = etree.Element(xml.TAG.root)
    if map_data:
        root.set('map', json.dumps(map_data))
    export_map = Storage()
    get_exported = export_map.get
    reference_map = []
    all_references = []
    master = (not target)
    export_resource = self.__export_resource
    for record in self._rows:
        element = export_resource(record, rfields=rfields, dfields=dfields, parent=root, base_url=url, reference_map=reference_map, export_map=export_map, lazy=lazy, components=components, filters=filters, master=master, target=target, msince=msince, location_data=location_data, xmlformat=xmlformat)
        if (element is None):
            results -= 1
    if reference_map:
        all_references.extend(reference_map)
    ref_components = {}
    if rcomponents:
        for key in rcomponents:
            if (':' in key):
                (tn, alias) = key.rsplit(':', 1)
                if (tn in ref_components):
                    ref_components[tn].append(alias)
                else:
                    ref_components[tn] = [alias]
    define_resource = current.s3db.resource
    REF = xml.ATTRIBUTE.ref
    depth = (maxdepth if dereference else 0)
    while (reference_map and depth):
        depth -= 1
        load_map = {}
        for ref in reference_map:
            if (('table' in ref) and ('id' in ref)):
                tname = ref['table']
                ids = ref['id']
                if (not isinstance(ids, list)):
                    ids = [ids]
                exported = get_exported(tname, [])
                ids = [x for x in ids if (x not in exported)]
                if (not ids):
                    continue
                if (tname in load_map):
                    ids = [x for x in ids if (x not in load_map[tname])]
                    load_map[tname] += ids
                else:
                    load_map[tname] = ids
        reference_map = []
        for tablename in load_map:
            load_list = load_map[tablename]
            if filters:
                filter_vars = filters.get(tablename, None)
            else:
                filter_vars = None
            components = self.components_to_export(tablename, ref_components.get(tablename))
            (prefix, name) = tablename.split('_', 1)
            rresource = define_resource(tablename, components=components, id=load_list, vars=filter_vars)
            table = rresource.table
            if base_url:
                url = ('%s/%s/%s' % (base_url, prefix, name))
            else:
                url = ('/%s/%s' % (prefix, name))
            (rfields, dfields) = rresource.split_fields(data=fields, references=references)
            if xmlformat:
                (include, exclude) = xmlformat.get_fields(rresource.tablename)
            else:
                (include, exclude) = (None, None)
            rresource.load(fields=include, skip=exclude, limit=None, virtual=False, cacheable=True)
            export_resource = rresource.__export_resource
            for record in rresource:
                element = export_resource(record, rfields=rfields, dfields=dfields, parent=root, base_url=url, reference_map=reference_map, export_map=export_map, components=components, lazy=lazy, filters=filters, master=False, target=target, location_data=location_data, xmlformat=xmlformat)
                if (element is not None):
                    element.set(REF, 'True')
            if reference_map:
                all_references.extend(reference_map)
    if lazy:
        for (renderer, element, attr, f) in lazy:
            renderer.render_node(element, attr, f)
    if all_references:
        xml.latlon(all_references)
    tree = xml.tree(None, root=root, domain=xml.domain, url=base_url, results=results, start=start, limit=limit, maxbounds=maxbounds)
    self.results = results
    return tree
