Method: REXML::Element#get_text

Defined in:
lib/rexml/element.rb

#get_text(path = nil) ⇒ Object

:call-seq:

get_text(xpath = nil) -> text_node or nil

Returns the first text node child in a specified element, if it exists, nil otherwise.

With no argument, returns the first text node from self:

d = REXML::Document.new "<p>some text <b>this is bold!</b> more text</p>"
d.root.get_text.class # => REXML::Text
d.root.get_text       # => "some text "

With argument xpath, returns the first text node from the element that matches xpath:

d.root.get_text(1) # => "this is bold!"


1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'lib/rexml/element.rb', line 1045

def get_text path = nil
  rv = nil
  if path
    element = @elements[ path ]
    rv = element.get_text unless element.nil?
  else
    rv = @children.find { |node| node.kind_of? Text }
  end
  rv
end