Class | REXML::Element |
In: |
lib/xmpp4r/rexmladdons.rb
|
Parent: | Object |
this class adds a few helper methods to REXML::Element
# File lib/xmpp4r/rexmladdons.rb, line 92 92: def self.import(xmlelement) 93: self.new(xmlelement.name).import(xmlelement) 94: end
Deletes one or more children elements, not just one like REXML::Element#delete_element
# File lib/xmpp4r/rexmladdons.rb, line 99 99: def delete_elements(element) 100: while(delete_element(element)) do end 101: end
Returns first element of name e
# File lib/xmpp4r/rexmladdons.rb, line 49 49: def first_element(e) 50: each_element(e) { |el| return el } 51: return nil 52: end
Returns text of first element of name e
# File lib/xmpp4r/rexmladdons.rb, line 56 56: def first_element_text(e) 57: el = first_element(e) 58: if el 59: return el.text 60: else 61: return nil 62: end 63: end
import this element’s children and attributes
# File lib/xmpp4r/rexmladdons.rb, line 76 76: def import(xmlelement) 77: if @name and @name != xmlelement.name 78: raise "Trying to import an #{xmlelement.name} to a #{@name} !" 79: end 80: add_attributes(xmlelement.attributes.clone) 81: @context = xmlelement.context 82: xmlelement.each do |e| 83: if e.kind_of? REXML::Element 84: typed_add(e.deep_clone) 85: else # text element, probably. 86: add(e.clone) 87: end 88: end 89: self 90: end
Replaces or add a child element of name e with text t.
# File lib/xmpp4r/rexmladdons.rb, line 35 35: def replace_element_text(e, t) 36: el = first_element(e) 37: if el.nil? 38: el = REXML::Element::new(e) 39: add_element(el) 40: end 41: if t 42: el.text = t 43: end 44: self 45: 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 70 70: def typed_add(e) 71: add(e) 72: end