Module: AXML
- Extended by:
- AXML
- Defined in:
- lib/axml.rb,
lib/axml/el.rb,
lib/axml/libxml.rb,
lib/axml/autoload.rb
Defined Under Namespace
Modules: Autoload Classes: El, LibXML, XMLParser
Constant Summary collapse
- DEFAULTS =
note that if Autoload must find a suitable parser, it will be set as the :parser default to be used for future reference.
{:keep_blanks => false, :parser => nil}
- PREFERRED =
[:xmlparser, :libxml, :libxml_sax, :rexml]
- CLASS_MAPPINGS =
{:xmlparser => 'XMLParser', :libxml => 'LibXML', :libxml_sax => 'libXMLSax', :rexml => 'REXML' }
- WARN =
{:rexml => "Using REXML as parser! This is very slow on large docs!\nCall the method AXML::Autoload.install_instructions for help installing\nsomething FASTER!", }
Instance Method Summary collapse
-
#parse(arg, opts = {}) ⇒ Object
returns the root node opts: :parser => :xmlparser || :libxml :keep_blanks => false.
-
#parse_file(file, opts = {}) ⇒ Object
Returns the root node.
-
#xml?(string) ⇒ Boolean
returns true if the first nonblank char is a ‘<’.
Instance Method Details
#parse(arg, opts = {}) ⇒ Object
returns the root node opts:
:parser => :xmlparser || :libxml
:keep_blanks => false
arg may be a string of xml, an io object, or a filename if the first non-blank character of the string is ‘<’ then it is considered xml. If you want to be sure, you can use parse_file or parse_string. Gets the parser with AXML::Autoload.parser! Will set the default parser to the first one found unless a parser is provided.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/axml.rb', line 24 def parse(arg, opts={}) opts = DEFAULTS.merge opts parser = AXML::Autoload.parser!(opts[:parser]) method = if arg.is_a?(String) if xml?(arg) :parse_string else unless File.exist? arg messg = "thinking this is a file: #{arg}\nbut file doesn't exist!" raise RuntimeError, messg end :parse_file end elsif arg.is_a?(IO) || arg.is_a?(StringIO) :parse_io else raise ArgumentError, "can deal with filenames, Strings, StringIO and IO objects.\nDon't know how to work with object of class: #{arg.class}" end parser.send(method, arg, opts) end |
#parse_file(file, opts = {}) ⇒ Object
Returns the root node. Opens the filename given and calls parse_io on it
48 49 50 51 52 |
# File 'lib/axml.rb', line 48 def parse_file(file, opts={}) # :nodoc: opts = DEFAULTS.merge opts parser = AXML::Autoload.parser!(opts[:parser]) File.open(file) {|fh| parser.parse_io(fh, opts) } end |
#xml?(string) ⇒ Boolean
returns true if the first nonblank char is a ‘<’
55 56 57 58 |
# File 'lib/axml.rb', line 55 def xml?(string) first_real_char = string.match(/[^\s]/m) !first_real_char.nil? && first_real_char[0] == '<' end |