Sha256: 92651728b0fa63ab34e4507c7a1b2d1710174d2ca3bf2fb2c3d1c232c4d22017

Contents?: true

Size: 699 Bytes

Versions: 1

Compression:

Stored size: 699 Bytes

Contents

require 'nokogiri'
require 'json'

class Graphml2Json

  def self.generate(xml_string) 
    doc = Nokogiri::XML(xml_string)
    graph = doc.xpath('//xmlns:graph')
    nodes = graph.xpath('//xmlns:node')
    edges = graph.xpath('//xmlns:edge')

    @mapping = {}
    @json_nodes = []

    @graph = {}
    @graph[:nodes] = [] 
    @graph[:edges] = [] 

    nodes.each_with_index do |n, index|
      @mapping[n[:id]] = index 
      @graph[:nodes] << {:name => index}
    end

    @json_edges = [] 
    edges.each do |e|
      source = @mapping[e[:source]]
      target = @mapping[e[:target]]
      @graph[:edges] << { :source => source, :target => target }
    end

    JSON.generate(@graph)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphml2json-0.0.1 lib/graphml2json/graphml2json.rb