// description of your code here
require 'xml/saxdriver'
@flag_item = false
@@finaldata = []
@@vars = []
@@positionSpace = []
@@currentName = []
def reset_vals
@@product_id = nil
@@name = nil
end
def check_position_space(name,ch)
if @@positionSpace == 'ASIN'
@@product_id = ch
elsif @@positionSpace == 'ItemAttributesTitle'
@@name = ch
end
end
class TestHandler < XML::SAX::HandlerBase
attr_accessor :data
def startDocument
@@data = []
end
def startElement(name, attr)
@flag_item = true if name == 'Item'
@@positionSpace = '' if name == 'Item'
if @flag_item == true and name != 'Item'
@@positionSpace = @@positionSpace + name
elsif name == 'Item'
reset_vals
end
@@currentName = name
end
def endElement(name)
if @flag_item == true and name != 'Item'
lenName = name.length
@@positionSpace = @@positionSpace[0, @@positionSpace.length - lenName]
end
if name == 'Item'
@@finaldata << @@data.to_s
@@data = []
end
@flag_item = false if name == 'Item'
end
def characters(ch, start, length)
check_position_space(@@currentName, ch[start, length])
end
end
def run_amazon_import(datafile)
@@datafile = datafile
p = XML::SAX::Helpers::ParserFactory.makeParser("XML::Parser::SAXDriver")
h = TestHandler.new
p.setDocumentHandler(h)
p.setDTDHandler(h)
p.setEntityResolver(h)
p.setErrorHandler(h)
begin
p.parse("#{RAILS_ROOT}/data/" + datafile + ".xml")
rescue XML::SAX::SAXParseException
p(["ParseError", $!.getSystemId, $!.getLineNumber, $!.getMessage])
end
end