def read(string):
dom = parseString(string)<EOL>if dom.getElementsByTagName("<STR_LIT>"):<EOL><INDENT>G = graph()<EOL><DEDENT>elif dom.getElementsByTagName("<STR_LIT>"):<EOL><INDENT>G = digraph()<EOL><DEDENT>elif dom.getElementsByTagName("<STR_LIT>"):<EOL><INDENT>return read_hypergraph(string)<EOL><DEDENT>else:<EOL><INDENT>raise InvalidGraphType<EOL><DEDENT>for each_node in dom.getElementsByTagName("<STR_LIT>"):<EOL><INDENT>G.add_node(each_node.getAttribute('<STR_LIT:id>'))<EOL>for each_attr in each_node.getElementsByTagName("<STR_LIT>"):<EOL><INDENT>G.add_node_attribute(each_node.getAttribute('<STR_LIT:id>'),<EOL>(each_attr.getAttribute('<STR_LIT>'),<EOL>each_attr.getAttribute('<STR_LIT:value>')))<EOL><DEDENT><DEDENT>for each_edge in dom.getElementsByTagName("<STR_LIT>"):<EOL><INDENT>if (not G.has_edge((each_edge.getAttribute('<STR_LIT>'), each_edge.getAttribute('<STR_LIT:to>')))):<EOL><INDENT>G.add_edge((each_edge.getAttribute('<STR_LIT>'), each_edge.getAttribute('<STR_LIT:to>')),wt = float(each_edge.getAttribute('<STR_LIT>')), label = each_edge.getAttribute('<STR_LIT:label>'))<EOL><DEDENT>for each_attr in each_edge.getElementsByTagName("<STR_LIT>"):<EOL><INDENT>attr_tuple = (each_attr.getAttribute('<STR_LIT>'), each_attr.getAttribute('<STR_LIT:value>'))<EOL>if (attr_tuple not in G.edge_attributes((each_edge.getAttribute('<STR_LIT>'),each_edge.getAttribute('<STR_LIT:to>')))):<EOL><INDENT>G.add_edge_attribute((each_edge.getAttribute('<STR_LIT>'),each_edge.getAttribute('<STR_LIT:to>')), attr_tuple)<EOL><DEDENT><DEDENT><DEDENT>return G<EOL>
Read a graph from a XML document and return it. Nodes and edges specified in the input will
be added to the current graph.

@type  string: string
@param string: Input string in XML format specifying a graph.

@rtype: graph
@return: Graph