Source: fileloader.js

// Functions needed to load a file containing graph data or to convert graph structures

/**
 * Loads the graph structure from a GraphML(XML) file.
 * @param {HTMLDocument} xml - HTMLDocument according to GraphML standard.
 * @returns {{nodes, links}} Graph data structure.
 */
function loadGraphML(xml) {
    var nodes = HTMLElementsToArray(xml.documentElement.getElementsByTagName("node"));
    var links = HTMLElementsToArray(xml.documentElement.getElementsByTagName("edge"));

    return {nodes: nodes, links: links};
}

/**
 * Converts a collection of html-elements of the same type into an array of objects.
 * @param {NodeListOf<Element>} collection - Collection containing html-elements of the same type.
 * @returns {Array} Array of objects.
 */
function HTMLElementsToArray(collection) {
    var htmlelements = Array.from(collection);
    var output = [];

    htmlelements.forEach(function (value) {
        var elements = Array.from(value.getElementsByTagName("data"));
        var attributes = value.getAttributeNames();
        var entry = {};

        attributes.forEach(function (value2) {
            entry[value2] = value.getAttribute(value2);
        });

        elements.forEach(function (value3) {
            entry[value3.getAttribute("key")] = value3.innerHTML;
        });

        output.push(entry);
    });

    return output;
}

/**
 * Converts a read in graph structure to a Network X graph object.
 * @param {Object} graph - Graph structure to convert
 * @returns {*} Network X graph object.
 */
function convertGraphToNetworkXFormat(graph) {
    var G = new jsnx.Graph();
    for(var i = 0; i < graph.nodes.length; i++) {
        var node = graph.nodes[i];
        G.addNode(node.id, node);
    }
    for (var i = 0; i < graph.links.length; i++) {
        var edge = graph.links[i];
        G.addEdge(edge.source, edge.target, edge);
    }
    return G;
}