Module: XML2JSON

Defined in:
lib/xml2json.rb,
lib/xml2json/version.rb

Defined Under Namespace

Classes: InvalidXML

Constant Summary collapse

VERSION =
"0.4.5"

Class Method Summary collapse

Class Method Details

.config {|configuration| ... } ⇒ Object

Yields:



89
90
91
# File 'lib/xml2json.rb', line 89

def self.config
  yield configuration if block_given?
end

.configurationObject



85
86
87
# File 'lib/xml2json.rb', line 85

def self.configuration
  @configuration ||= Configuration.new
end

.namespaced_node_name(node) ⇒ Object



73
74
75
# File 'lib/xml2json.rb', line 73

def self.namespaced_node_name node
  "#{prefix(node)}#{node.name}"
end

.node2json(node) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/xml2json.rb', line 44

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) unless key == pluralized_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



64
65
66
67
68
69
70
71
# File 'lib/xml2json.rb', line 64

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



9
10
11
# File 'lib/xml2json.rb', line 9

def self.parse xml
  parse_to_hash(xml).to_json
end

.parse_attributes(node) ⇒ Object



40
41
42
# File 'lib/xml2json.rb', line 40

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



27
28
29
30
31
32
33
# File 'lib/xml2json.rb', line 27

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



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/xml2json.rb', line 13

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



77
78
79
80
81
82
83
# File 'lib/xml2json.rb', line 77

def self.prefix node
  if !node.namespace.nil? && !node.namespace.prefix.nil? && !node.namespace.prefix.strip.empty?
    "#{node.namespace.prefix}:"
  else
    ""
  end
end

.resetObject



93
94
95
# File 'lib/xml2json.rb', line 93

def self.reset
  @configuration = Configuration.new
end

.text_hash(node) ⇒ Object



35
36
37
38
# File 'lib/xml2json.rb', line 35

def self.text_hash(node)
  return {} if node.text.strip.empty?
  { self.configuration.text_key => node.text }
end