Sha256: 46e70fe0968adbd4f4f233247f1bf368655ae054746429441d3f6064416fae4e

Contents?: true

Size: 1.52 KB

Versions: 20

Compression:

Stored size: 1.52 KB

Contents

$:.unshift("./lib")
require "rubygems"
require "nokogiri"

class ActiveResourceXML < Nokogiri::XML::SAX::Document
  attr_accessor :doc
  def initialize
    @types = []
    @values = []
    @doc = nil
  end

  def parse_attrs(attrs=[])
    type = "hash"
    attrs.each do |attr|
      case attr[0]
      when "type"
        type = attr[1]
      when "nil"
        @nil = true
      end
    end
    return type
  end

  def start_element name, attrs = []
    type = parse_attrs(attrs)
    unless @nil
      @types.push type
      case type
      when "array"
        @values.push Array.new
      when "hash"
        @values.push Hash.new
      else 
        @values.push ""
      end
    end
  end

  def characters(s)
    return if s =~ /^[\s\n\t]*$/
    @types[-1] = "string"
    @values[-1] =  s
  end

  def end_element name
    if @nil
      @nil = false
    else
      value = @values.pop
      type = @types.pop

      if @values[-1] 
        if @types[-1] == "array"
          @values[-1].push value
        else
          @values[-1][name] = value
        end
      else
        @doc = { name => value }
      end
    end
  end

end

class Message
  def self.parse(xml="")
    source= ActiveResourceXML.new
    parser = Nokogiri::XML::SAX::Parser.new(source)
    parser.parse(xml)
    return source
  end
end


xml = File.read("/Users/bru/Downloads/socialcast.xml")
start_time = Time.new
document = Message.parse(xml)
end_time = Time.new

puts "File parsed in #{end_time - start_time} seconds"
puts "Found #{document.doc["messages"].size} messages"



Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
socialcastr-0.2.19 examples/free_parse_messages.rb
socialcastr-0.2.18 examples/free_parse_messages.rb
socialcastr-0.2.17 examples/free_parse_messages.rb
socialcastr-0.2.16 examples/free_parse_messages.rb
socialcastr-0.2.15 examples/free_parse_messages.rb
socialcastr-0.2.14 examples/free_parse_messages.rb
socialcastr-0.2.13 examples/free_parse_messages.rb
socialcastr-0.2.12 examples/free_parse_messages.rb
socialcastr-0.2.11 examples/free_parse_messages.rb
socialcastr-0.2.10 examples/free_parse_messages.rb
socialcastr-0.2.9 examples/free_parse_messages.rb
socialcastr-0.2.8 examples/free_parse_messages.rb
socialcastr-0.2.7 examples/free_parse_messages.rb
socialcastr-0.2.6 examples/free_parse_messages.rb
socialcastr-0.2.5 examples/free_parse_messages.rb
socialcastr-0.2.4 examples/free_parse_messages.rb
socialcastr-0.2.3 examples/free_parse_messages.rb
socialcastr-0.2.2 examples/free_parse_messages.rb
socialcastr-0.2.1 examples/free_parse_messages.rb
socialcastr-0.2.0 examples/free_parse_messages.rb