Class: XmlNode
- Defined in:
- lib/vendor/xml_node/benchmark/bench_generation.rb,
lib/vendor/xml_node/lib/xml_node.rb
Defined Under Namespace
Classes: List
Instance Attribute Summary collapse
-
#child_nodes ⇒ Object
Returns the value of attribute child_nodes.
-
#element ⇒ Object
readonly
Returns the value of attribute element.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(elem) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #cdata ⇒ Object
- #cdata=(value) ⇒ Object
- #children ⇒ Object
- #find(scope, xpath) ⇒ Object
-
#initialize(node, *args) ⇒ XmlNode
constructor
Allows for very pretty xml generation akin to xml builder.
- #name ⇒ Object
-
#namespace(*args) ⇒ Object
Add a namespace to the node Example.
- #text ⇒ Object
- #text=(value) ⇒ Object
- #to_s ⇒ Object
-
#to_xml ⇒ Object
Use to get pretty formatted xml including DECL instructions.
- #to_xml_as_array ⇒ Object
- #to_xml_element ⇒ Object
- #to_xml_no_format ⇒ Object
Constructor Details
#initialize(node, *args) ⇒ XmlNode
Allows for very pretty xml generation akin to xml builder.
Example:
# Create an atom like document
doc = Document.new
doc.root = XmlNode.new 'feed' do |feed|
feed << XmlNode.new('id', 'tag:atom.com,2007:1')
feed << XmlNode.new('title', 'Atom test feed')
feed << XmlNode.new('author') do ||
<< XmlNode.new("name", "tobi")
<< XmlNode.new("email", "[email protected]")
end
feed << XmlNode.new('entry') do |entry|
entry << XmlNode.new('title', 'First post')
entry << XmlNode.new('summary', 'Lorem ipsum', :type => 'xhtml')
entry << XmlNode.new('created_at', Time.now)
end
feed << XmlNode.new('dc:published', Time.now)
end
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 106 def initialize(node, *args) @element = if node.is_a?(REXML::Element) node else REXML::Element.new(node) end @child_nodes = {} if attributes = args.last.is_a?(Hash) ? args.pop : nil attributes.each { |k,v| @element.add_attribute(k.to_s, v.to_xml_value) } end if !args[0].nil? @element.text = args[0].to_xml_value end if block_given? yield self end end |
Instance Attribute Details
#child_nodes ⇒ Object
Returns the value of attribute child_nodes.
52 53 54 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 52 def child_nodes @child_nodes end |
#element ⇒ Object (readonly)
Returns the value of attribute element.
53 54 55 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 53 def element @element end |
Class Method Details
Instance Method Details
#<<(elem) ⇒ Object
193 194 195 196 197 198 199 200 201 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 193 def <<(elem) case elem when nil then return when Array elem.each { |e| @element << e.to_xml_element } else @element << elem.to_xml_element end end |
#[](key) ⇒ Object
140 141 142 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 140 def [](key) @element.attributes[key] end |
#[]=(key, value) ⇒ Object
136 137 138 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 136 def []=(key, value) @element.attributes[key.to_s] = value.to_xml_value end |
#cdata ⇒ Object
167 168 169 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 167 def cdata @element.cdatas.first.to_s end |
#cdata=(value) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 155 def cdata=(value) new_cdata = REXML::CData.new( value ) @element.children.each do |c| if c.is_a?(REXML::CData) return @element.replace_child(c,new_cdata) end end @element << new_cdata rescue RuntimeError => e @element << REXML::Text.new(e.) end |
#children ⇒ Object
132 133 134 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 132 def children XmlNode::List.new(self) end |
#find(scope, xpath) ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 183 def find(scope, xpath) case scope when :first elem = @element.elements[xpath] elem.nil? ? nil : child_nodes[elem] ||= XmlNode.new(elem) when :all @element.elements.to_a(xpath).collect { |e| child_nodes[e] ||= XmlNode.new(e) } end end |
#name ⇒ Object
171 172 173 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 171 def name @element.name end |
#namespace(*args) ⇒ Object
Add a namespace to the node Example
node.namespace 'http://www.w3.org/2005/Atom'
node.namespace :opensearch, 'http://a9.com/-/spec/opensearch/1.1/'
150 151 152 153 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 150 def namespace(*args) args[0] = args[0].to_s if args[0].is_a?(Symbol) @element.add_namespace(*args) end |
#text ⇒ Object
179 180 181 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 179 def text @element.text end |
#text=(value) ⇒ Object
175 176 177 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 175 def text=(value) @element.text = REXML::Text.new( value ) end |
#to_s ⇒ Object
207 208 209 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 207 def to_s @element.to_s end |
#to_xml ⇒ Object
Use to get pretty formatted xml including DECL instructions
213 214 215 216 217 218 219 220 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 213 def to_xml xml = [] document = REXML::Document.new document << REXML::XMLDecl.new('1.0') document << @element document.write( xml, 0) xml.join end |
#to_xml_as_array ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/vendor/xml_node/benchmark/bench_generation.rb', line 6 def to_xml_as_array xml = [] document = REXML::Document.new document << REXML::XMLDecl.new('1.0') document << @element document.write( xml, 0) xml.to_s end |
#to_xml_element ⇒ Object
203 204 205 |
# File 'lib/vendor/xml_node/lib/xml_node.rb', line 203 def to_xml_element @element end |
#to_xml_no_format ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/vendor/xml_node/benchmark/bench_generation.rb', line 15 def to_xml_no_format xml = '' document = REXML::Document.new document << REXML::XMLDecl.new('1.0') document << @element document.write( xml) xml end |