def dict_to_etree(d, root):
    u"""Converts a dict to lxml.etree object.

    >>> dict_to_etree({'root': {'#text': 'node_text', '@attr': 'val'}}, etree.Element('root')) # doctest: +ELLIPSIS
    <Element root at 0x...>

    :param dict d: dict representing the XML tree
    :param etree.Element root: XML node which will be assigned the resulting tree
    :returns: Textual representation of the XML tree
    :rtype: str
    """
    def _to_etree(d, node):
        if d is None or len(d) == 0:
            return
        elif isinstance(d, basestring):
            node.text = d
        elif isinstance(d, dict):
            for k, v in d.items():
                assert isinstance(k, basestring)
                if k.startswith('#'):
                    assert k == '#text'
                    assert isinstance(v, basestring)
                    node.text = v
                elif k.startswith('@'):
                    assert isinstance(v, basestring)
                    node.set(k[1:], v)
                elif isinstance(v, list):
                    # No matter the child count, their parent will be the same.
                    sub_elem = etree.SubElement(node, k)

                    for child_num, e in enumerate(v):
                        if e is None:
                            if child_num == 0:
                                # Found the first occurrence of an empty child,
                                # skip creating of its XML repr, since it would be
                                # the same as ``sub_element`` higher up.
                                continue
                            # A list with None element means an empty child node
                            # in its parent, thus, recreating tags we have to go
                            # up one level.
                            # <node><child/></child></node> <=> {'node': 'child': [None, None]}
                            _to_etree(node, k)
                        else:
                            # If this isn't first child and it's a complex
                            # value (dict), we need to check if it's value
                            # is equivalent to None.
                            if child_num != 0 and not (isinstance(e, dict) and not all(e.values())):
                                # At least one child was None, we have to create
                                # a new parent-node, which will not be empty.
                                sub_elem = etree.SubElement(node, k)
                            _to_etree(e, sub_elem)
                else:
                    _to_etree(v, etree.SubElement(node, k))
        elif etree.iselement(d):
            # Supports the case, when we got an empty child and want to recreate it.
            etree.SubElement(d, node)
        else:
            raise AttributeError('Argument is neither dict nor basestring.')

    _to_etree(d, root)
    return root
u"""Converts a dict to lxml.etree object.

    >>> dict_to_etree({'root': {'#text': 'node_text', '@attr': 'val'}}, etree.Element('root')) # doctest: +ELLIPSIS
    <Element root at 0x...>

    :param dict d: dict representing the XML tree
    :param etree.Element root: XML node which will be assigned the resulting tree
    :returns: Textual representation of the XML tree
    :rtype: str