docstr: Serialize graph G in GraphML to the stream.
docstr_tok: ["Serialize","graph","G","in","GraphML","to","the","stream","."]
code: def add_graph_element(self, G):
      """
      Serialize graph G in GraphML to the stream.
      """
      if G.is_directed():
          default_edge_type = "directed"
      else:
          default_edge_type = "undirected"
  
      graphid = G.graph.pop("id", None)
      if graphid is None:
          graph_element = self._xml.element(
              "graph", edgedefault=default_edge_type
          )
      else:
          graph_element = self._xml.element(
              "graph", edgedefault=default_edge_type, id=graphid
          )
  
      # gather attributes types for the whole graph
      # to find the most general numeric format needed.
      # Then pass through attributes to create key_id for each.
      graphdata = {
          k: v
          for k, v in G.graph.items()
          if k not in ("node_default", "edge_default")
      }
      node_default = G.graph.get("node_default", {})
      edge_default = G.graph.get("edge_default", {})
      # Graph attributes
      for k, v in graphdata.items():
          self.attribute_types[(str(k), "graph")].add(type(v))
      for k, v in graphdata.items():
          element_type = self.get_xml_type(
              self.attr_type(k, "graph", v)
          )
          self.get_key(str(k), element_type, "graph", None)
      # Nodes and data
      for node, d in G.nodes(data=True):
          for k, v in d.items():
              self.attribute_types[(str(k), "node")].add(type(v))
      for node, d in G.nodes(data=True):
          for k, v in d.items():
              T = self.get_xml_type(self.attr_type(k, "node", v))
              self.get_key(str(k), T, "node", node_default.get(k))
      # Edges and data
      if G.is_multigraph():
          for u, v, ekey, d in G.edges(keys=True, data=True):
              for k, v in d.items():
                  self.attribute_types[(str(k), "edge")].add(type(v))
          for u, v, ekey, d in G.edges(keys=True, data=True):
              for k, v in d.items():
                  T = self.get_xml_type(self.attr_type(k, "edge", v))
                  self.get_key(str(k), T, "edge", edge_default.get(k))
      else:
          for u, v, d in G.edges(data=True):
              for k, v in d.items():
                  self.attribute_types[(str(k), "edge")].add(type(v))
          for u, v, d in G.edges(data=True):
              for k, v in d.items():
                  T = self.get_xml_type(self.attr_type(k, "edge", v))
                  self.get_key(str(k), T, "edge", edge_default.get(k))
  
      # Now add attribute keys to the xml file
      for key in self.xml:
          self._xml.write(key, pretty_print=self._prettyprint)
  
      # The incremental_writer writes each node/edge as it is created
      incremental_writer = IncrementalElement(
          self._xml, self._prettyprint
      )
      with graph_element:
          self.add_attributes(
              "graph", incremental_writer, graphdata, {}
          )
          self.add_nodes(G, incremental_writer)  # adds attributes too
          self.add_edges(G, incremental_writer)
  
imports: import warnings
  from collections import defaultdict
  import networkx
  from networkx.utils import open_file
label_desc: Use image node_id 2 for calling the GraphMLWriterLxml obj's underlying member method code with example usage: obj.add_graph_element(G) without return types