Skip to content

Instantly share code, notes, and snippets.

@bryanl
Created January 26, 2009 16:12
Show Gist options
  • Select an option

  • Save bryanl/52856 to your computer and use it in GitHub Desktop.

Select an option

Save bryanl/52856 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'couchrest'
class Array
def to_h
Hash[*self.flatten]
end
end
class CveDoc < Nokogiri::XML::SAX::Document
def start_document
@location = {}
@count = 0
@status = {}
@items = []
end
def start_element(name, attrs)
case name
when "item"
@location[:item] = true
@item = {:type => "Cve"}
@item[:attrs] = attrs.to_h
@item[:name] = attrs[3]
when "status"
@location[:status] = true
when "desc"
@location[:desc] = true
when "refs"
@location[:refs] = true
@item[:refs] = []
when "ref"
@location[:ref] = true
@current_ref = attrs
end
end
def end_element(name)
case name
when "item"
@location[:item] = false
@items << @item
@count += 1
save_items if @count % 2500 == 0
when "status"
@location[:status] = false
when "desc"
@location[:desc] = false
when "refs"
@location[:refs] = false
when "ref"
@location[:ref] = false
@item[:refs] <<@current_ref.to_h
@current_ref = nil
end
end
def end_document
save_items
end
def characters(stuff)
if @location[:status]
@item[:status] = stuff
elsif @location[:desc]
@item[:desc] = stuff
elsif @location[:ref]
@current_ref << "text"
@current_ref << stuff
end
end
private
def save_items
db = CouchRest.database!("http://127.0.0.1:5984/cve-test")
response = db.bulk_save(@items)
@items = []
end
end
cve = "allitems.xml"
xml = Nokogiri::XML::SAX::Parser.new(CveDoc.new)
xml.parse_file(cve)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment