Class: XMLCodec::XMLStreamObjectParser
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- XMLCodec::XMLStreamObjectParser
- Defined in:
- lib/stream_object_parser.rb
Overview
This is a XML Stream parser that returns ruby objects for whole elements. To do this a class must be defined as descending from XMLElement and having set elname or elnames. To use it all you have to do is define a listener that responds to methods of the form el_<element name> and define the importers for the elements as subclasses of XMLElement.
The listener will be passed XMLSOParserElement objects. The two relevant methods for it’s use are XMLSOParserElement#get_object and XMLSOParserElement#consume.
Instance Method Summary collapse
-
#characters(text) ⇒ Object
:nodoc:.
-
#end_element(name) ⇒ Object
:nodoc:.
-
#initialize(base_element, listener = nil) ⇒ XMLStreamObjectParser
constructor
Create a new parser with a listener.
-
#parse(text) ⇒ Object
Parse the text with the stream parser calling the listener on any events that it listens to.
-
#start_element(name, attrs) ⇒ Object
:nodoc:.
-
#top_element ⇒ Object
Get the current top element of the parse.
Constructor Details
#initialize(base_element, listener = nil) ⇒ XMLStreamObjectParser
Create a new parser with a listener.
65 66 67 68 69 70 71 72 73 |
# File 'lib/stream_object_parser.rb', line 65 def initialize(base_element, listener=nil) @base_element = base_element @listener = listener @children = Hash.new([]) @currel = 0 @elements = [XMLSOParserElement.new(nil, nil, nil, nil, nil, 0)] @id = 0 @top_element = nil end |
Instance Method Details
#characters(text) ⇒ Object
:nodoc:
113 114 115 |
# File 'lib/stream_object_parser.rb', line 113 def characters(text) #:nodoc: curr_element.add_child(text) end |
#end_element(name) ⇒ Object
:nodoc:
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/stream_object_parser.rb', line 117 def end_element(name) #:nodoc: obj = curr_element if @listener.respond_to?("el_"+name) @listener.send("el_"+name, obj) end if not obj.consumed real_obj = obj.get_object if prev_element && real_obj prev_element.add_child(real_obj) end @top_element = obj end @elements.pop @currel -= 1 end |
#parse(text) ⇒ Object
Parse the text with the stream parser calling the listener on any events that it listens to.
95 96 97 98 |
# File 'lib/stream_object_parser.rb', line 95 def parse(text) parser = Nokogiri::XML::SAX::Parser.new(self) parser.parse(text) end |
#start_element(name, attrs) ⇒ Object
:nodoc:
106 107 108 109 110 111 |
# File 'lib/stream_object_parser.rb', line 106 def start_element(name, attrs) #:nodoc: @elements << XMLSOParserElement.new(name, attrs, get_elclass(name), curr_element, next_id, curr_element.depth+1) @currel += 1 end |
#top_element ⇒ Object
Get the current top element of the parse. This is usually used to get the root at the end of the parse.
102 103 104 |
# File 'lib/stream_object_parser.rb', line 102 def top_element @top_element.get_object if @top_element end |