3
# = XmlMini Nokogiri implementation
5
module XmlMini_Nokogiri #:nodoc:
8
# Parse an XML Document string into a simple hash using libxml / nokogiri.
10
# XML Document string to parse
15
doc = Nokogiri::XML(string) { |cfg| cfg.noblanks }
16
raise doc.errors.first if doc.errors.length > 0
29
CONTENT_ROOT = '__content__'
31
# Convert XML document to hash
34
# Hash to merge the converted element into.
35
def to_hash(hash = {})
36
attributes = attributes_as_hash
38
hash[name] = [hash[name]].flatten
39
hash[name] << attributes
41
hash[name] ||= attributes
44
children.each { |child|
45
next if child.blank? && 'file' != self['type']
47
if child.text? || child.cdata?
48
(attributes[CONTENT_ROOT] ||= '') << child.content
52
child.to_hash attributes
58
def attributes_as_hash
59
Hash[*(attribute_nodes.map { |node|
60
[node.node_name, node.value]
66
Nokogiri::XML::Document.send(:include, Conversions::Document)
67
Nokogiri::XML::Node.send(:include, Conversions::Node)