Returns a DOM element containing the XML representation of the invoice
        @return:Element

def to_xml(self):
        '''
        Returns a DOM element containing the XML representation of the invoice
        @return:Element
        '''
        if not len(self.groups):
            raise InvoiceError("An invoice must have at least one group " \
                                   "of lines.")

        for n, v in {"identifier": self.identifier,
                     "name": self.name, "currency": self.currency,
                     "seller": self.seller, "buyer":self.buyer,
                     "status": self.status, "date": self.date,
                     "due_date": self.due_date, "identifier": self.identifier,
                     "mentions": self.mentions, 'domain': self.domain}.items():
            if is_empty_or_none(v):
                raise InvoiceError("'%s' attribute cannot be empty or " \
                                       "None." % n)

        total_invoice = self.total
        total_payments = sum([payment.amount for payment in self.payments])
        if total_payments > total_invoice:
            raise InvoiceError('The sum of the payments declared ' \
                                   '(%f %s) can\'t be superior to the ' \
                                   'total of the invoice (%f %s).' %
                                   (total_payments, self.currency,
                                    total_invoice, self.currency))
        if self.status == INVOICE_PAID and total_payments < total_invoice:
            raise InvoiceError('The invoice can only be marked as paid ' \
                                   'if the sum of its payments (%f %s) is ' \
                                   'equal to its total (%f %s).' %
                                   (total_payments, self.currency,
                                    total_invoice, self.currency))

        doc = Document()
        root = doc.createElement("invoice")
        root.setAttribute('xmlns', DEFAULT_NAMESPACE)
        root.setAttribute("domain", self.domain)
        root.setAttribute("version", XMLi_VERSION)
        root.setAttribute("agent", AGENT)

        #Adding custom elements
        super(Invoice, self).to_xml(root)

        self._create_text_node(root, "id", self.identifier)
        self._create_text_node(root, "name", self.name, True)
        self._create_text_node(root, "description", self.description, True)
        self._create_text_node(root, "date", self.date)
        self._create_text_node(root, "dueDate", self.due_date)
        self._create_text_node(root, "currency", self.currency)
        self._create_text_node(root, "status", self.status)
        root.appendChild(self.seller.to_xml("seller"))
        root.appendChild(self.buyer.to_xml("buyer"))
        if self.__shipping:
            root.appendChild(self.shipping.to_xml())
        self._create_text_node(root, "terms", self.terms, True)
        self._create_text_node(root, "mentions", self.mentions, True)

        if len(self.__payments):
            payments = doc.createElement("payments")
            for payment in self.__payments:
                payments.appendChild(payment.to_xml())
            root.appendChild(payments)

        if len(self.deliveries):
            deliveries = doc.createElement('deliveries')
            for delivery in self.__deliveries:
                deliveries.appendChild(delivery.to_xml())
            root.appendChild(deliveries)

        body = doc.createElement("body")
        root.appendChild(body)

        groups = doc.createElement("groups")
        body.appendChild(groups)
        for group in self.__groups:
            if not issubclass(group.__class__, Group):
                raise InvoiceError('group of type %s is not an instance ' \
                                       'or a subclass of %s' %
                                       (group.__class__.__name__,
                                        Group.__name__))
            groups.appendChild(group.to_xml())

        return root