Module: XML2JSON
- Defined in:
- lib/xml2json.rb,
lib/xml2json/version.rb
Defined Under Namespace
Classes: Configuration, InvalidXML
Constant Summary
collapse
- VERSION =
"0.4.4"
Class Method Summary
collapse
Class Method Details
.config {|configuration| ... } ⇒ Object
98
99
100
|
# File 'lib/xml2json.rb', line 98
def self.config
yield configuration if block_given?
end
|
.configuration ⇒ Object
94
95
96
|
# File 'lib/xml2json.rb', line 94
def self.configuration
@configuration ||= Configuration.new
end
|
.namespaced_node_name(node) ⇒ Object
72
73
74
|
# File 'lib/xml2json.rb', line 72
def self.namespaced_node_name node
"#{prefix(node)}#{node.name}"
end
|
.node2json(node) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/xml2json.rb', line 43
def self.node2json node
node.element_children.each_with_object({}) do |child, hash|
key = namespaced_node_name child
pluralized_key = key.pluralize
if hash.has_key?(key)
node_to_nodes!(hash, child)
hash.delete(key)
hash[pluralized_key] << parse_node(child)
else
if hash.has_key?(pluralized_key)
hash[pluralized_key] << parse_node(child)
else
hash[key] = parse_node(child)
end
end
end
end
|
.node_to_nodes!(hash, node) ⇒ Object
63
64
65
66
67
68
69
70
|
# File 'lib/xml2json.rb', line 63
def self.node_to_nodes! hash, node
key = namespaced_node_name(node)
if !hash[key].is_a?(Array)
tmp = hash[key]
hash[key.pluralize] = []
hash[key.pluralize] << tmp
end
end
|
.parse(xml) ⇒ Object
8
9
10
|
# File 'lib/xml2json.rb', line 8
def self.parse xml
parse_to_hash(xml).to_json
end
|
.parse_attributes(node) ⇒ Object
39
40
41
|
# File 'lib/xml2json.rb', line 39
def self.parse_attributes(node)
node.attributes.empty? ? {} : { self.configuration.attributes_key => Hash[node.attributes.map { |k, v| [k, v.value] } ]}
end
|
.parse_node(node) ⇒ Object
26
27
28
29
30
31
32
|
# File 'lib/xml2json.rb', line 26
def self.parse_node(node)
if node.element_children.count > 0
parse_attributes(node).merge(node2json(node))
else
(node.attributes.empty? ? node.text : parse_attributes(node).merge(text_hash(node)))
end
end
|
.parse_to_hash(xml) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/xml2json.rb', line 12
def self.parse_to_hash xml
begin
doc = Nokogiri.XML(xml) { |config| config.strict }
rescue Nokogiri::XML::SyntaxError
raise InvalidXML.new
end
root = doc.root
hash = { root.name => parse_node(root) }
hash[root.name] = { self.configuration.namespaces_key => root.namespaces }.merge(hash[root.name]) unless root.namespaces.empty?
hash
end
|
.prefix(node) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/xml2json.rb', line 76
def self.prefix node
if !node.namespace.nil? && !node.namespace.prefix.nil? && !node.namespace.prefix.strip.empty?
"#{node.namespace.prefix}:"
else
""
end
end
|
.reset ⇒ Object
102
103
104
|
# File 'lib/xml2json.rb', line 102
def self.reset
@configuration = Configuration.new
end
|
.text_hash(node) ⇒ Object
34
35
36
37
|
# File 'lib/xml2json.rb', line 34
def self.text_hash(node)
return {} if node.text.strip.empty?
{ self.configuration.text_key => node.text }
end
|