Module: HashUtilsXML

Included in:
Array, Hash
Defined in:
lib/hashutils.rb

Instance Method Summary collapse

Instance Method Details

#from_xml(element) ⇒ Object

from_xml start



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hashutils.rb', line 64

def from_xml(element)
  out = nil

  if element.is_a?String
    element = REXML::Document.new(element)
    element = element.root
  end
  
  if !element.has_elements? and !element.has_attributes?
    out = element.text 
  else    
    if element.has_attributes?
      out = {}
      process_xml_attributes(element.attributes, out)
    end
    
    if element.has_elements?
      if potential_xml_array?(element.children)
        out = []
        process_xml_elements_as_array(element.children, out)
      else
        out = out || {}
        process_xml_elements(element.children, out)
      end
    end
  end    
  
  out
end

#from_xml!(element) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hashutils.rb', line 94

def from_xml!(element)
  out = self.from_xml(element)
  if out.class == self.class
    self.clear
    if self.is_a?Hash
      self.merge!(out) 
      self.xml_attributes = out.xml_attributes
    end
    if self.is_a?Array
      self.concat(out)
      self.xml_name = out.xml_name
    end
  end
end

#to_xml(args = {:name => nil, :pretty => false}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hashutils.rb', line 18

def to_xml(args = {:name => nil, :pretty => false})
  tab = (args[:tab].nil?)? "" : args[:tab]
   
  build_attrs = Proc.new do |k, v|
    attributes = (self.respond_to?"xml_attributes" and !self.xml_attributes.nil?) ? self.xml_attributes : []
    out = ""
    out += " " + k.to_s + "=\"" + v.to_s + "\"" if attributes.select{|x| x == k}.length > 0
    out
  end

  build_body = Proc.new do |k, v|
    attributes = (self.respond_to?"xml_attributes" and !self.xml_attributes.nil?) ? self.xml_attributes : []
    out = ""
    if attributes.select{|x| x == k}.length == 0
      new_args = args.merge({:tab => tab, :name => k})
      out += v.to_xml(new_args) if v.respond_to?"to_xml"
      out += ((args[:pretty])? tab : "") + @@lt + k.to_s + @@gt + v.to_s + @@clt + k.to_s + @@gt unless v.respond_to?"to_xml"
      out += @@new_line if args[:pretty]
    end
    out
  end
 
  out = ""
  if !args[:name].nil?
    out += ((args[:pretty])? tab : "") + @@lt + args[:name].to_s 
    self.each_pair {|k, v| out += build_attrs.call(k, v, args)} if self.respond_to?"each_pair"
    out += @@gt
  end
  
  orig_tab = tab
  if !args[:name].nil? and args[:pretty]
    out += @@new_line
    tab += @@default_tab
  end
  
  self.each_pair {|k, v| out += build_body.call(k, v)} if self.respond_to?"each_pair"
  self.each {|v| out += build_body.call((@xml_name.nil?) ? "item" : @xml_name, v)} if !self.respond_to?"each_pair"
  
  out += ((args[:pretty])? orig_tab : "") + @@clt + args[:name].to_s + @@gt if !args[:name].nil?
  out
end