def parse(xml, options = {})
if xml.is_a?(XML::Node)
node = xml
else
if xml.is_a?(XML::Document)
node = xml.root
else
node = XML::Parser.string(xml).parse.root
end
root = node.name == tag_name
end
namespace = @namespace || (node.namespaces && node.namespaces.default)
namespace = "#{DEFAULT_NS}:#{namespace}" if namespace
unless xpath = options[:xpath]
xpath = root ? '/' : './/'
xpath += "#{DEFAULT_NS}:" if namespace
xpath += tag_name
end
nodes = node.find(xpath, Array(namespace))
collection = nodes.collect do |n|
obj = new
attributes.each do |attr|
obj.send("#{attr.method_name}=",
attr.from_xml_node(n, namespace))
end
elements.each do |elem|
obj.send("#{elem.method_name}=",
elem.from_xml_node(n, namespace))
end
obj.send("#{@content}=", n.content) if @content
obj.class.after_parse_callbacks.each { |callback| callback.call(obj) }
obj
end
nodes = nil
if options[:single] || root
collection.first
else
collection
end
end