def gen_body(self):
        """Create the <body> etree element of an RS3 file (contains segments
        and groups) given a DGParentedTree.
        """
        # We need to sort the treepositions, as RSTTool relies on <segment>
        # elements to be in linear order of the EDUs in the text.
        for treepos in sorted(self.treepositions):
            node = self.dgtree[treepos]
            node_id = self.get_node_id(treepos)
            node_type = get_node_type(node)

            if node_type in (TreeNodeTypes.leaf_node,
                             TreeNodeTypes.relation_node):
                relname, parent_id = self.get_relname_and_parent(treepos)

                attrib_list = [('id', node_id)]
                if parent_id is not None:
                    attrib_list.extend([('parent', parent_id), ('relname', relname)])

                if node_type == TreeNodeTypes.leaf_node:
                    self.body['segments'].append(E('segment', node, OrderedDict(attrib_list)))

                else:  # node_type == TreeNodeTypes.relation_node:
                    group_type = self.get_group_type(treepos)
                    # insert 'type' attrib between 'id' and 'parent'
                    attrib_list.insert(1, ('type', group_type))
                    self.body['groups'].append(E('group', OrderedDict(attrib_list)))