Sha256: d49c04493f1c4097e4dca4178b427c41ede62a8805fa49f14bd821c5cccf6525
Contents?: true
Size: 1.61 KB
Versions: 1
Compression:
Stored size: 1.61 KB
Contents
require 'rubygems' require 'nokogiri' module Atomic module Parser def self.included(base) base.extend(ClassMethods) end module ClassMethods def parse(xml) reader = Nokogiri::XML::Reader.from_memory(xml, nil, nil, Nokogiri::XML::PARSE_NOBLANKS) object = new object.deserialize(reader.read) object end end Node = Struct.new("Node", :depth, :name, :uri, :attributes, :text) def deserialize(reader) stack = [] start_depth = reader.depth loop do node = Node.new(reader.depth - start_depth, reader.local_name, reader.namespace_uri, reader.attributes) if (stack.empty? || node.depth > stack.last.depth) # child if(reader.value?) stack.last.text = reader.value unless stack.empty? reader.read else redo if handle_open_element(node,reader) stack.push(node) end elsif(node.depth < stack.last.depth) # parent handle_close_element(stack.last) stack.pop else # sibling handle_close_element(stack.last) stack.pop unless stack.empty? redo if handle_open_element(node,reader) stack.push(node) end end break if stack.empty? reader.read end return self end def handle_open_element(node, reader) progressed = false puts("Open Element - #{node.inspect}") return progressed end def handle_close_element(node) puts("Close Element - #{node.inspect}") end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
exempla-atomic-0.0.12 | lib/atomic/parser.rb |