An open API service indexing awesome lists of open source software.

https://github.com/anderspitman/graphml-js

GraphML parser for javascript
https://github.com/anderspitman/graphml-js

Last synced: 9 months ago
JSON representation

GraphML parser for javascript

Awesome Lists containing this project

README

          

# Intro

`graphml-js` is a simple [GraphML](http://graphml.graphdrawing.org/) parser written in TypeScript, for use in
JavaScript projects. It is far from feature complete, and currently only includes the
minimum functionality I need for a project I'm working on. However, it should be
easy to add features in the future as needed by me or others.

# Example Usage

`graphml-js` takes in a GraphML file, and returns a `Graph` object, which contains
`nodes` and `edges` properties, which are both arrays of `Node` and `Edge` objects,
respectively.

The following example is included in the examples directory.

Given the following graph taken from the [GraphML Primer](http://graphml.graphdrawing.org/primer/graphml-primer.html):

`primer.graphml`:
```xml


yellow




green



blue


red



turquoise


1.0


1.0


2.0





1.1

```

And the following node code:

`simple_example.js`:
```javascript
var graphml = require('graphml-js');
var fs = require('fs');

var graphmlText = fs.readFileSync('primer.graphml');
var parser = new graphml.GraphMLParser();

parser.parse(graphmlText, function(err, graphs) {
console.log(graphs[0]);
});
```

The output is:

```
Graph {
nodes:
[ Node { _id: 'n0', _attributes: [Object] },
Node { _id: 'n1', _attributes: {} },
Node { _id: 'n2', _attributes: [Object] },
Node { _id: 'n3', _attributes: [Object] },
Node { _id: 'n4', _attributes: {} },
Node { _id: 'n5', _attributes: [Object] } ],
edges:
[ Edge { _id: 'e0', _attributes: [Object], _source: 'n0', _target: 'n2' },
Edge { _id: 'e1', _attributes: [Object], _source: 'n0', _target: 'n1' },
Edge { _id: 'e2', _attributes: [Object], _source: 'n1', _target: 'n3' },
Edge { _id: 'e3', _attributes: {}, _source: 'n3', _target: 'n2' },
Edge { _id: 'e4', _attributes: {}, _source: 'n2', _target: 'n4' },
Edge { _id: 'e5', _attributes: {}, _source: 'n3', _target: 'n5' },
Edge { _id: 'e6', _attributes: [Object], _source: 'n5', _target: 'n4' } ] }
```

# Contribution

## Build from source
The output directory is `dist`.

```bash
$ npm install
$ gulp
```

## Run tests

```bash
$ gulp test
```