# How to read a graph from a GML file

**URL:** <https://discourse.julialang.org/t/how-to-read-a-graph-from-a-gml-file/68539>\
**Category:** Graphs\
**Tags:** lightgraphs, graphio\
**Created:** [September 21, 2021, 4:37pm UTC](https://discourse.julialang.org/t/how-to-read-a-graph-from-a-gml-file/68539 "2021-09-21T16:37:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [September 21, 2021, 4:37pm UTC](https://discourse.julialang.org/t/how-to-read-a-graph-from-a-gml-file/68539/1 "2021-09-21T16:37:52Z")

</div>

I cannot figure out how to get a graph definition from a GML file.  
I downloaded the following file [https://github.com/empet/Datasets/blob/master/gstrang\_descendants.gml](https://github.com/empet/Datasets/blob/master/gstrang_descendants.gml)  
and tried with the following code:

```julia
using LightGraphs, ParserCombinator, GraphIO 
G = loadgraph("gstrang_descendants.gml", GMLFormat())
#G = loadgraph("gstrang_descendants.gml", "graph", GMLFormat())

```

and both cases (with “graph” or without it) give the same error:

```julia
Graph graph not found

Stacktrace:
  [1] error(s::String)
    @ Base .\error.jl:33
  [2] loadgml(io::IOStream, gname::String)
    @ GraphIO.GML C:\Users\empet\.julia\packages\GraphIO\IgxnP\src\GML\Gml.jl:40
  [3] loadgraph
    @ C:\Users\empet\.julia\packages\GraphIO\IgxnP\src\GML\Gml.jl:95 [inlined]
  [4] #117
    @ C:\Users\empet\.julia\packages\LightGraphs\IgJif\src\persistence\common.jl:15 [inlined]
  [5] open(::LightGraphs.var"#117#118"{String, GraphIO.GML.GMLFormat}, ::String, ::Vararg{String, N} where N; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base .\io.jl:330
  [6] open
    @ .\io.jl:328 [inlined]
  [7] loadgraph(fn::String, gname::String, format::GraphIO.GML.GMLFormat)
    @ LightGraphs C:\Users\empet\.julia\packages\LightGraphs\IgJif\src\persistence\common.jl:14
  [8] top-level scope
    @ In[12]:1
  [9] eval
    @ .\boot.jl:360 [inlined]
 [10] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base .\loading.jl:1094

```

My code is based on this information:

```julia
?LightGraphs.loadgraph
loadgraph(file, gname="graph", format=LGFormat())
Read a graph named gname from file in the format format.

Implementation Notes
gname is graph-format dependent and is only used if the file contains multiple graphs; if the file format does not support multiple graphs, this value is ignored. The default value may change in the future.

```

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [September 22, 2021, 6:03am UTC](https://discourse.julialang.org/t/how-to-read-a-graph-from-a-gml-file/68539/2 "2021-09-22T06:03:38Z")

</div>

This is the solution:

```julia
using LightGraphs, ParserCombinator, GraphIO
graph = loadgraph("gstrang_descendants.gml", "digraph", GraphIO.GML.GMLFormat())

```
