Class: Jabber::Protocol::XMLElement
- Inherits:
-
Object
- Object
- Jabber::Protocol::XMLElement
- Defined in:
- lib/jabber4r/protocol.rb
Overview
Utility class to create valid XML strings
Instance Attribute Summary collapse
-
#parent ⇒ Object
The parent XMLElement.
Instance Method Summary collapse
-
#add_attribute(attrib, value) ⇒ Object
Adds an attribute to this element.
-
#add_cdata(cdata) ⇒ Object
Adds cdata to this element.
-
#add_child(tag, attributes = {}) ⇒ Object
Adds a child to this element of the supplied tag.
-
#add_data(data) ⇒ Object
Adds data to this element.
-
#add_xml(xml) ⇒ Object
Adds arbitrary XML data to this object.
-
#initialize(tag, attributes = {}) ⇒ XMLElement
constructor
Construct an XMLElement for the supplied tag and attributes.
-
#set_namespace(ns) ⇒ Object
Sets the namespace for this tag.
-
#to_parent ⇒ Object
Returns the parent element.
-
#to_s ⇒ Object
Climbs to the top of this elements parent tree and then returns the to_xml XML string.
-
#to_xml(format, indent = 0) ⇒ Object
Recursively builds the XML string by traversing this element’s children.
Constructor Details
#initialize(tag, attributes = {}) ⇒ XMLElement
Construct an XMLElement for the supplied tag and attributes
- tag
- String
-
XML tag
- attributes
- Hash = {}
-
The attribute hash=value
887 888 889 890 891 892 |
# File 'lib/jabber4r/protocol.rb', line 887 def initialize(tag, attributes={}) @tag = tag @elements = [] @attributes = attributes @data = "" end |
Instance Attribute Details
#parent ⇒ Object
The parent XMLElement
880 881 882 |
# File 'lib/jabber4r/protocol.rb', line 880 def parent @parent end |
Instance Method Details
#add_attribute(attrib, value) ⇒ Object
Adds an attribute to this element
- attrib
- String
-
The attribute name
- value
- String
-
The attribute value
- return
- Jabber::Protocol::XMLElement
-
self for chaining
901 902 903 904 |
# File 'lib/jabber4r/protocol.rb', line 901 def add_attribute(attrib, value) @attributes[attrib]=value self end |
#add_cdata(cdata) ⇒ Object
Adds cdata to this element
- cdata
- String
-
The cdata to add
- return
- Jabber::Protocol::XMLElement
-
self for chaining
934 935 936 937 |
# File 'lib/jabber4r/protocol.rb', line 934 def add_cdata(cdata) @data += "<![CDATA[#{cdata.to_s}]]>" self end |
#add_child(tag, attributes = {}) ⇒ Object
Adds a child to this element of the supplied tag
- tag
- String
-
The element tag
- attributes
- Hash = {}
-
The attributes hash=value
- return
- Jabber::Protocol::XMLElement
-
newly created child element
955 956 957 958 959 960 |
# File 'lib/jabber4r/protocol.rb', line 955 def add_child(tag, attributes={}) child = XMLElement.new(tag, attributes) child.parent = self @elements << child return child end |
#add_data(data) ⇒ Object
Adds data to this element
- data
- String
-
The data to add
- return
- Jabber::Protocol::XMLElement
-
self for chaining
912 913 914 915 |
# File 'lib/jabber4r/protocol.rb', line 912 def add_data(data) @data += data.to_s self end |
#add_xml(xml) ⇒ Object
Adds arbitrary XML data to this object
- xml
- String
-
the xml to add
967 968 969 |
# File 'lib/jabber4r/protocol.rb', line 967 def add_xml(xml) @xml = xml end |
#set_namespace(ns) ⇒ Object
Sets the namespace for this tag
- ns
- String
-
The namespace
- return
- Jabber::Protocol::XMLElement
-
self for chaining
923 924 925 926 |
# File 'lib/jabber4r/protocol.rb', line 923 def set_namespace(ns) @tag+=":#{ns}" self end |
#to_parent ⇒ Object
Returns the parent element
- return
- Jabber::Protocol::XMLElement
-
The parent XMLElement
944 945 946 |
# File 'lib/jabber4r/protocol.rb', line 944 def to_parent @parent end |
#to_s ⇒ Object
Climbs to the top of this elements parent tree and then returns the to_xml XML string.
- return
- String
-
The XML string of this element (from the topmost parent).
1005 1006 1007 1008 |
# File 'lib/jabber4r/protocol.rb', line 1005 def to_s return @parent.to_s if @parent return to_xml(true) end |
#to_xml(format, indent = 0) ⇒ Object
Recursively builds the XML string by traversing this element’s children.
- format
- Boolean
-
True to pretty-print (format) the output string
- indent
- Integer = 0
-
The indent level (recursively more)
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 |
# File 'lib/jabber4r/protocol.rb', line 978 def to_xml(format, indent=0) result = "" result += " "*indent if format result += "<#{@tag}" @attributes.each {|attrib, value| result += (' '+attrib.to_s+'="'+value.to_s+'"') } if @data=="" and @elements.size==0 result +="/>" result +="\n" if format return result end result += ">" result += "\n" if format and @data=="" result += @data if @data!="" @elements.each {|element| result+=element.to_xml(format, indent+4)} result += @xml if not @xml.nil? result += " "*indent if format and @data=="" result+="</#{@tag}>" result+="\n" if format return result end |