def write(G):
<EOL>grxml = Document()<EOL>if (type(G) == graph):<EOL><INDENT>grxmlr = grxml.createElement('<STR_LIT>')<EOL><DEDENT>elif (type(G) == digraph ):<EOL><INDENT>grxmlr = grxml.createElement('<STR_LIT>')<EOL><DEDENT>elif (type(G) == hypergraph ):<EOL><INDENT>return write_hypergraph(G)<EOL><DEDENT>else:<EOL><INDENT>raise InvalidGraphType<EOL><DEDENT>grxml.appendChild(grxmlr)<EOL>for each_node in G.nodes():<EOL><INDENT>node = grxml.createElement('<STR_LIT>')<EOL>node.setAttribute('<STR_LIT:id>', str(each_node))<EOL>grxmlr.appendChild(node)<EOL>for each_attr in G.node_attributes(each_node):<EOL><INDENT>attr = grxml.createElement('<STR_LIT>')<EOL>attr.setAttribute('<STR_LIT>', each_attr[<NUM_LIT:0>])<EOL>attr.setAttribute('<STR_LIT:value>', each_attr[<NUM_LIT:1>])<EOL>node.appendChild(attr)<EOL><DEDENT><DEDENT>for edge_from, edge_to in G.edges():<EOL><INDENT>edge = grxml.createElement('<STR_LIT>')<EOL>edge.setAttribute('<STR_LIT>', str(edge_from))<EOL>edge.setAttribute('<STR_LIT:to>', str(edge_to))<EOL>edge.setAttribute('<STR_LIT>', str(G.edge_weight((edge_from, edge_to))))<EOL>edge.setAttribute('<STR_LIT:label>', str(G.edge_label((edge_from, edge_to))))<EOL>grxmlr.appendChild(edge)<EOL>for attr_name, attr_value in G.edge_attributes((edge_from, edge_to)):<EOL><INDENT>attr = grxml.createElement('<STR_LIT>')<EOL>attr.setAttribute('<STR_LIT>', attr_name)<EOL>attr.setAttribute('<STR_LIT:value>', attr_value)<EOL>edge.appendChild(attr)<EOL><DEDENT><DEDENT>return grxml.toprettyxml()<EOL>
Return a string specifying the given graph as a XML document.

@type  G: graph
@param G: Graph.

@rtype:  string
@return: String specifying the graph as a XML document.