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'sattr.nameandattr.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:Employeespelling and;/,separators are all split), or from an edge'slabelattribute. - 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; atokiofeature addsAsyncGraphmlReaderover anyAsyncBufRead.
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.
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");
# Ok::<(), graphml::GraphmlError>(())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);
# Ok::<(), graphml::GraphmlError>(())With the tokio feature, AsyncGraphmlReader::new(reader).next_record().await
does the same over an AsyncBufRead.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.