Skip to main content

read_graphml

Function read_graphml 

Source
pub fn read_graphml<R: Read>(input: R) -> IgraphResult<GraphmlGraph>
Expand description

Read a graph from GraphML format.

Parses the first <graph> element in the input. Extracts structural elements (<node>, <edge>) and attribute data (<key> definitions and <data> elements). Attributes are stored on the returned graph using the Graph attribute system.

ยงExamples

use rust_igraph::{Graph, read_graphml, write_graphml, AttributeValue};

let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
  <key id="d0" for="node" attr.name="label" attr.type="string"/>
  <key id="d1" for="edge" attr.name="weight" attr.type="double"/>
  <graph id="G" edgedefault="undirected">
    <node id="Alice"><data key="d0">Alice</data></node>
    <node id="Bob"><data key="d0">Bob</data></node>
    <edge source="Alice" target="Bob"><data key="d1">1.5</data></edge>
  </graph>
</graphml>"#;

let result = read_graphml(xml.as_bytes()).unwrap();
assert_eq!(result.graph.vcount(), 2);
assert_eq!(result.graph.ecount(), 1);
assert_eq!(
    result.graph.vertex_attribute("label", 0).and_then(|v| v.as_str()),
    Some("Alice"),
);
assert_eq!(
    result.graph.edge_attribute("weight", 0).and_then(|v| v.as_f64()),
    Some(1.5),
);