Class: XmlNode

Inherits:
Object show all
Defined in:
lib/action_controller/vendor/xml_node.rb

Overview

SimpleXML like xml parser. Written by leon breet from the ruby on rails Mailing list

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, options = {}) ⇒ XmlNode

Returns a new instance of XmlNode.



7
8
9
10
11
# File 'lib/action_controller/vendor/xml_node.rb', line 7

def initialize(node, options = {})
  @node = node
  @children = {}
  @raise_errors = options[:raise_errors]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/action_controller/vendor/xml_node.rb', line 47

def method_missing(name, *args)
  name = name.to_s
  nodes = nil
  @node.each_element(name) do |element|
    nodes ||= XmlNodeList.new
    nodes << (@children[element] ||= XmlNode.new(element))
  end
  nodes
end

Instance Attribute Details

#nodeObject (readonly)

:nodoc:



5
6
7
# File 'lib/action_controller/vendor/xml_node.rb', line 5

def node
  @node
end

Class Method Details

.from_xml(xml_or_io) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/action_controller/vendor/xml_node.rb', line 13

def self.from_xml(xml_or_io)
  document = REXML::Document.new(xml_or_io)
  if document.root 
    XmlNode.new(document.root) 
  else
    XmlNode.new(document) 
  end
end

Instance Method Details

#<<(node) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/action_controller/vendor/xml_node.rb', line 57

def <<(node)
  if node.is_a? REXML::Node
    child = node
  elsif node.respond_to? :node
    child = node.node
  end
  @node.add_element child
  @children[child] ||= XmlNode.new(child)
end

#[](name) ⇒ Object



67
68
69
# File 'lib/action_controller/vendor/xml_node.rb', line 67

def [](name)
  @node.attributes[name.to_s]
end

#[]=(name, value) ⇒ Object



71
72
73
# File 'lib/action_controller/vendor/xml_node.rb', line 71

def []=(name, value)
  @node.attributes[name.to_s] = value
end

#node_encodingObject



22
23
24
# File 'lib/action_controller/vendor/xml_node.rb', line 22

def node_encoding
  @node.encoding
end

#node_nameObject



26
27
28
# File 'lib/action_controller/vendor/xml_node.rb', line 26

def node_name
  @node.name
end

#node_valueObject



30
31
32
# File 'lib/action_controller/vendor/xml_node.rb', line 30

def node_value
  @node.text
end

#node_value=(value) ⇒ Object



34
35
36
# File 'lib/action_controller/vendor/xml_node.rb', line 34

def node_value=(value)
  @node.text = value
end

#to_iObject



79
80
81
# File 'lib/action_controller/vendor/xml_node.rb', line 79

def to_i
  to_s.to_i
end

#to_sObject



75
76
77
# File 'lib/action_controller/vendor/xml_node.rb', line 75

def to_s
  @node.to_s
end

#xpath(expr) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/action_controller/vendor/xml_node.rb', line 38

def xpath(expr)
  matches = nil
  REXML::XPath.each(@node, expr) do |element|
    matches ||= XmlNodeList.new
    matches << (@children[element] ||= XmlNode.new(element))
  end
  matches
end