<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _add_domains_xml(self, document):
 """ Generates the XML elements for allowed domains. """

        for domain, attrs in self.domains.items():
            domain_element = document.createElement('allow-access-from')
            domain_element.setAttribute('domain', domain)
            if attrs['to_ports'] is not None:
                domain_element.setAttribute(
                    'to-ports',
                    ','.join(attrs['to_ports'])
                )
            if not attrs['secure']:
                domain_element.setAttribute('secure', 'false')
            document.documentElement.appendChild(domain_element)