class XmlNode

Attributes

child_nodes[RW]
element[R]

Public Class Methods

new(node, *args) { |self| ... } click to toggle source

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 |author|
    author << XmlNode.new("name", "tobi")
    author << XmlNode.new("email", "tobi@gmail.com")
  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
# 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
parse(xml) click to toggle source
# File lib/vendor/xml_node/lib/xml_node.rb, line 128
def self.parse(xml)
  self.new(REXML::Document.new(xml).root)
end

Public Instance Methods

<<(elem) click to toggle source
# 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) click to toggle source
# File lib/vendor/xml_node/lib/xml_node.rb, line 140
def [](key)
  @element.attributes[key]
end
[]=(key, value) click to toggle source
# 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() click to toggle source
# File lib/vendor/xml_node/lib/xml_node.rb, line 167
def cdata
  @element.cdatas.first.to_s
end
cdata=(value) click to toggle source
# 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.message)
end
children() click to toggle source
# File lib/vendor/xml_node/lib/xml_node.rb, line 132
def children
  XmlNode::List.new(self)
end
find(scope, xpath) click to toggle source
# 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() click to toggle source
# File lib/vendor/xml_node/lib/xml_node.rb, line 171
def name
  @element.name
end
namespace(*args) click to toggle source

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/'
# 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() click to toggle source
# File lib/vendor/xml_node/lib/xml_node.rb, line 179
def text
  @element.text
end
text=(value) click to toggle source
# File lib/vendor/xml_node/lib/xml_node.rb, line 175
def text=(value)
  @element.text = REXML::Text.new( value )    
end
to_s() click to toggle source
# File lib/vendor/xml_node/lib/xml_node.rb, line 207
def to_s
  @element.to_s
end
to_xml() click to toggle source

Use to get pretty formatted xml including DECL instructions

# 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() click to toggle source
# 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() click to toggle source
# File lib/vendor/xml_node/lib/xml_node.rb, line 203
def to_xml_element
  @element
end
to_xml_no_format() click to toggle source
# 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