Class | REXML::Element |
In: |
lib/xmpp4r/rexmladdons.rb
|
Parent: | Object |
this class adds a few helper methods to REXML::Element
Deletes one or more children elements, not just one like REXML::Element#delete_element
# File lib/xmpp4r/rexmladdons.rb, line 76 76: def delete_elements(element) 77: while(delete_element(element)) do end 78: end
Returns first element of name e
# File lib/xmpp4r/rexmladdons.rb, line 30 30: def first_element(e) 31: each_element(e) { |el| return el } 32: return nil 33: end
Returns text of first element of name e
# File lib/xmpp4r/rexmladdons.rb, line 37 37: def first_element_text(e) 38: el = first_element(e) 39: if el 40: return el.text 41: else 42: return nil 43: end 44: end
import this element’s children and attributes
# File lib/xmpp4r/rexmladdons.rb, line 57 57: def import(xmlelement) 58: if @name and @name != xmlelement.name 59: raise "Trying to import an #{xmlelement.name} to a #{@name} !" 60: end 61: add_attributes(xmlelement.attributes.clone) 62: @context = xmlelement.context 63: xmlelement.each do |e| 64: if e.kind_of? REXML::Element 65: typed_add(e.deep_clone) 66: else # text element, probably. 67: add(e.clone) 68: end 69: end 70: self 71: end
Replaces or add a child element of name e with text t.
# File lib/xmpp4r/rexmladdons.rb, line 16 16: def replace_element_text(e, t) 17: el = first_element(e) 18: if el.nil? 19: el = REXML::Element::new(e) 20: add_element(el) 21: end 22: if t 23: el.text = t 24: end 25: self 26: end
This method does exactly the same thing as add(), but it can be overriden by subclasses to provide on-the-fly object creations. For example, if you import a REXML::Element of name ‘plop’, and you have a Plop class that subclasses REXML::Element, with typed_add you can get your REXML::Element to be "magically" converted to Plop.
# File lib/xmpp4r/rexmladdons.rb, line 51 51: def typed_add(e) 52: add(e) 53: end