Methods
Constants
| CONTENT_ROOT | = | '__content__'.freeze |
Public Instance methods
Convert XML document to hash
| hash: | Hash to merge the converted element into. |
[ show source ]
# File activesupport/lib/active_support/xml_mini/nokogiri.rb, line 35
35: def to_hash(hash = {})
36: node_hash = {}
37:
38: # Insert node hash into parent hash correctly.
39: case hash[name]
40: when Array then hash[name] << node_hash
41: when Hash then hash[name] = [hash[name], node_hash]
42: when nil then hash[name] = node_hash
43: else raise "Unexpected error during hash insertion!"
44: end
45:
46: # Handle child elements
47: children.each do |c|
48: if c.element?
49: c.to_hash(node_hash)
50: elsif c.text? || c.cdata?
51: node_hash[CONTENT_ROOT] ||= ''
52: node_hash[CONTENT_ROOT] << c.content
53: end
54: end
55:
56: # Remove content node if it is blank and there are child tags
57: if node_hash.length > 1 && node_hash[CONTENT_ROOT].blank?
58: node_hash.delete(CONTENT_ROOT)
59: end
60:
61: # Handle attributes
62: attribute_nodes.each { |a| node_hash[a.node_name] = a.value }
63:
64: hash
65: end