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/libxml.rb, line 36
36: def to_hash(hash={})
37: node_hash = {}
38:
39: # Insert node hash into parent hash correctly.
40: case hash[name]
41: when Array then hash[name] << node_hash
42: when Hash then hash[name] = [hash[name], node_hash]
43: when nil then hash[name] = node_hash
44: else raise "Unexpected error during hash insertion!"
45: end
46:
47: # Handle child elements
48: each_child do |c|
49: if c.element?
50: c.to_hash(node_hash)
51: elsif c.text? || c.cdata?
52: node_hash[CONTENT_ROOT] ||= ''
53: node_hash[CONTENT_ROOT] << c.content
54: end
55: end
56:
57:
58: # Remove content node if it is blank
59: if node_hash.length > 1 && node_hash[CONTENT_ROOT].blank?
60: node_hash.delete(CONTENT_ROOT)
61: end
62:
63: # Handle attributes
64: each_attr { |a| node_hash[a.name] = a.value }
65:
66: hash
67: end