Class: XmlNodeStream::Parser
- Inherits:
-
Object
- Object
- XmlNodeStream::Parser
- Defined in:
- lib/xml_node_stream/parser.rb,
lib/xml_node_stream/parser/base.rb
Overview
The abstract parser class that wraps the actual parser implementation.
Defined Under Namespace
Modules: Base
Constant Summary collapse
- SUPPORTED_PARSERS =
[:nokogiri, :libxml, :rexml]
Class Method Summary collapse
-
.parse(io, &block) ⇒ Object
Parse the document specified in io.
-
.parser_name ⇒ Object
Get the name of the current parser.
-
.parser_name=(parser) ⇒ Object
Set the parser implementation.
Class Method Details
.parse(io, &block) ⇒ Object
Parse the document specified in io. This can be either a Stream, URI, Pathname, or String. If it is a String, it can either be a XML document, file system path, or URI. The parser will figure it out. If a block is given, it will be yielded to with each node as it is parsed.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/xml_node_stream/parser.rb', line 30 def parse (io, &block) close_stream = false if io.is_a?(String) if io.include?('<') and io.include?('>') io = StringIO.new(io) else io = open(io) end close_stream = true elsif io.is_a?(Pathname) io = io.open close_stream = true elsif io.is_a?(URI) io = io.open close_stream = true end begin parser = parser_class(parser_name).new(&block) parser.parse_stream(io) return parser.root ensure io.close if close_stream end end |
.parser_name ⇒ Object
Get the name of the current parser.
23 24 25 |
# File 'lib/xml_node_stream/parser.rb', line 23 def parser_name @parser_name ||= :rexml end |
.parser_name=(parser) ⇒ Object
Set the parser implementation. The parser argument should be one of :nokogiri, :libxml, or :rexml. If this method is not called, it will default to :rexml which is the slowest choice possible. If you set the parser to one of the other values, though, you’ll need to make sure you have the nokogiri gem or libxml-ruby gem installed.
16 17 18 19 20 |
# File 'lib/xml_node_stream/parser.rb', line 16 def parser_name= (parser) parser_sym = parser.to_sym raise ArgumentError.new("must be one of #{SUPPORTED_PARSERS.inspect}") unless SUPPORTED_PARSERS.include?(parser_sym) @parser_name = parser_sym end |