| 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 77
77: def self.import(xmlelement)
78: self.new.import(xmlelement)
79: end
Deletes one or more children elements, not just one like REXML::Element#delete_element
# File lib/xmpp4r/rexmladdons.rb, line 84
84: def delete_elements(element)
85: while(delete_element(element)) do end
86: end
Returns first element of name e
# File lib/xmpp4r/rexmladdons.rb, line 34
34: def first_element(e)
35: each_element(e) { |el| return el }
36: return nil
37: end
Returns text of first element of name e
# File lib/xmpp4r/rexmladdons.rb, line 41
41: def first_element_text(e)
42: el = first_element(e)
43: if el
44: return el.text
45: else
46: return nil
47: end
48: end
import this element’s children and attributes
# File lib/xmpp4r/rexmladdons.rb, line 61
61: def import(xmlelement)
62: if @name and @name != xmlelement.name
63: raise "Trying to import an #{xmlelement.name} to a #{@name} !"
64: end
65: add_attributes(xmlelement.attributes.clone)
66: @context = xmlelement.context
67: xmlelement.each do |e|
68: if e.kind_of? REXML::Element
69: typed_add(e.deep_clone)
70: else # text element, probably.
71: add(e.clone)
72: end
73: end
74: self
75: end
Replaces or add a child element of name e with text t.
# File lib/xmpp4r/rexmladdons.rb, line 20
20: def replace_element_text(e, t)
21: el = first_element(e)
22: if el.nil?
23: el = REXML::Element::new(e)
24: add_element(el)
25: end
26: if t
27: el.text = t
28: end
29: self
30: 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 55
55: def typed_add(e)
56: add(e)
57: end