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
)
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 hash
[name
] ||= attributes_as_hash
38 walker
= lambda
{ |memo
, parent
, child
, callback
|
39 next if child
.blank
? && 'file' != parent
['type']
42 (memo
[CONTENT_ROOT
] ||= '') << child
.content
48 child_hash
= child
.attributes_as_hash
50 memo
[name
] = [memo
[name
]].flatten
51 memo
[name
] << child_hash
53 memo
[name
] = child_hash
56 # Recusively walk children
57 child
.children
.each
{ |c
|
58 callback
.call(child_hash
, child
, c
, callback
)
62 children
.each
{ |c
| walker
.call(hash
[name
], self, c
, walker
) }
66 def attributes_as_hash
67 Hash
[*(attribute_nodes
.map
{ |node
|
68 [node
.node_name
, node
.value
]
74 Nokogiri
::XML::Document.send(:include, Conversions
::Document)
75 Nokogiri
::XML::Node.send(:include, Conversions
::Node)