8
9
10
11
12
13
14
15
16
17
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
59
60
61
62
63
64
|
# File 'lib/dynamics_crm/xml/message_parser.rb', line 8
def self.parse_key_value_pairs(parent_element)
h = {}
namespace_alias = parent_element.attributes.keys.first || "c"
parent_element.each_element do |key_value_pair|
key_element = key_value_pair.elements["#{namespace_alias}:key"]
key = key_element.text
value_element = key_value_pair.elements["#{namespace_alias}:value"]
value = value_element.text
begin
case value_element.attributes["type"]
when "b:OptionSetValue"
value = value_element.elements.first.text.to_i
when "d:boolean"
value = (value == "true")
when "d:decimal"
value = value.to_f
when "d:dateTime"
value = Time.parse(value)
when "b:EntityReference"
entity_ref = {}
value_element.each_element do |child|
entity_ref[child.name] = child.text
end
value = entity_ref
when "b:EntityCollection"
value = XML::EntityCollection.new(value_element)
when "d:EntityMetadata", /^d:\w*AttributeMetadata$/
value = value_element
when "d:ArrayOfEntityMetadata"
value = value_element.get_elements("d:EntityMetadata")
when "d:ArrayOfAttributeMetadata"
value = value_element.get_elements("d:AttributeMetadata")
when "b:EntityMetadataCollection"
value = value_element.get_elements("b:EntityMetadata")
when "b:AliasedValue"
value = value_element.elements["b:Value"].text
when "b:Money"
value = value_element.elements.first.text.to_f
when "d:int"
value = value.to_i
when "d:string", "d:guid"
end
rescue => e
puts e.message
end
h[key] = value
end
h
end
|