'Generates a GraphML XML file at the given path.'

def export(self, path, directed=False, encoding='utf-8'):

    import xml.etree.ElementTree as etree
    ns = '{http://graphml.graphdrawing.org/xmlns}'
    etree.register_namespace('', ns.strip('{}'))
    root = etree.Element((ns + 'graphml'))
    root.insert(0, etree.Element((ns + 'key'), **{'id': 'node_label', 'for': 'node', 'attr.name': 'label', 'attr.type': 'string'}))
    root.insert(0, etree.Element((ns + 'key'), **{'id': 'edge_weight', 'for': 'edge', 'attr.name': 'weight', 'attr.type': 'double'}))
    m = {}
    g = etree.SubElement(root, (ns + 'graph'), id='g', edgedefault=((directed and 'directed') or 'undirected'))
    for (i, n) in enumerate(self.graph.nodes):
        m[n.id] = ('node%s' % i)
        x = etree.SubElement(g, (ns + 'node'), id=m[n.id])
        x = etree.SubElement(x, (ns + 'data'), key='node_label')
        if (n.text and (n.text.string != n.id)):
            x.text = n.text.string
    for (i, e) in enumerate(self.graph.edges):
        x = etree.SubElement(g, (ns + 'edge'), id=('edge%s' % i), source=m[e.node1.id], target=m[e.node2.id])
        x = etree.SubElement(x, (ns + 'data'), key='edge_weight')
        x.text = ('%.3f' % e.weight)
    def indent(e, level=0):
        w = ('\n' + (level * '        '))
        if len(e):
            if ((not e.text) or (not e.text.strip())):
                e.text = (w + '        ')
            if ((not e.tail) or (not e.tail.strip())):
                e.tail = w
            for e in e:
                indent(e, (level + 1))
            if ((not e.tail) or (not e.tail.strip())):
                e.tail = w
        elif (level and ((not e.tail) or (not e.tail.strip()))):
            e.tail = w
    indent(root)
    tree = etree.ElementTree(root)
    tree.write(path, encoding=encoding)
