Skip to main content

Crate graphml

Crate graphml 

Source
Expand description

§graphml

Crates.io Downloads Documentation CI License

A streaming GraphML parser that yields owned property-graph nodes and edges one element at a time.

  • <key> declarations are honoured: every <data> resolves to its key’s attr.name and attr.type (kept as a type hint), and an undeclared key is an error rather than a silent drop.
  • Labels come from a key named label/labels (Neo4j’s :Person:Employee spelling and ;/, separators are all split), or from an edge’s label attribute.
  • Hierarchical GraphML (a <graph> nested in a <node>) is tolerated: the outer node is emitted, nested content is skipped.
  • Memory is bounded by the key table plus one open element, however large the document. Built on quick-xml; a tokio feature adds AsyncGraphmlReader over any AsyncBufRead.

The crate depends on no database driver, graph store, RDF model, or application framework. The optional property-graph-model feature adds field-for-field From conversions into that crate’s PgNode / PgEdge.

§Quick start

use graphml::parse_graphml;

let input = r#"<graphml>
  <key id="labels" for="node" attr.name="labels"/>
  <key id="age" for="node" attr.name="age" attr.type="int"/>
  <graph edgedefault="directed">
    <node id="alice"><data key="labels">:Person</data><data key="age">30</data></node>
    <node id="bob"><data key="labels">:Person</data></node>
    <edge source="alice" target="bob" label="KNOWS"/>
  </graph>
</graphml>"#;

let (nodes, edges) = parse_graphml(input)?;
assert_eq!(nodes[0].id, "alice");
assert_eq!(nodes[0].labels, ["Person"]);
assert_eq!(nodes[0].properties[0].name, "age");
assert_eq!(nodes[0].properties[0].type_hint.as_deref(), Some("int"));
assert_eq!(edges[0].label, "KNOWS");

§Streaming large documents

use graphml::{GraphmlReader, GraphmlRecord};

let input = r#"<graphml><graph><node id="a"/><edge source="a" target="a" label="SELF"/></graph></graphml>"#;
let mut edges = 0;
for record in GraphmlReader::new(input.as_bytes()) {
    if let GraphmlRecord::Edge(_) = record? {
        edges += 1;
    }
}
assert_eq!(edges, 1);

With the tokio feature, AsyncGraphmlReader::new(reader).next_record().await does the same over an AsyncBufRead.

§License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Structs§

Edge
A GraphML <edge>.
GraphmlReader
Streams <node> and <edge> records out of a GraphML document as it is read. Memory is bounded by the <key> table plus one element.
Node
A GraphML <node>.
Property
One <data> value with the declared key’s name and type.

Enums§

GraphmlError
Errors from parsing GraphML documents.
GraphmlRecord
One completed <node> or <edge>.
KeyDomain
Which elements a <key> applies to (for attribute).
KeyType
The declared attr.type of a <key>.

Functions§

parse_graphml
Parse a complete GraphML document into its nodes and edges.
parse_graphml_with
Parse a complete GraphML document, invoking on_node / on_edge for each record as it completes.