0
-# Locked down XmlSimple#xml_in_string
0
- # Same as xml_in but doesn't try to smartly shoot itself in the foot.
0
- def xml_in_string(string, options = nil)
0
- handle_options('in', options)
0
+# This is a derivitive work of XmlSimple 1.0.11
0
+# Author:: Joseph Holsten <joseph@josephholsten.com>
0
+# Copyright:: Copyright (c) 2008 Joseph Holsten
0
+# Copyright:: Copyright (c) 2003-2006 Maik Schmidt <contact@maik-schmidt.de>
0
+# License:: Distributes under the same terms as Ruby.
0
+ require 'rexml/document'
0
- result = collapse(@doc.root)
0
+ CONTENT_KEY = '__content__'
0
- if @options['keeproot']
0
- merge({}, @doc.root.name, result)
0
+ # Parse an XML Document string into a simple hash
0
+ # Same as XmlSimple::xml_in but doesn't shoot itself in the foot,
0
+ # and uses the defaults from ActiveSupport
0
+ # XML Document string to parse
0
+ def self.parse(string)
0
+ doc = REXML::Document.new(string)
0
+ merge_element!({}, doc.root)
0
+ # Convert an XML element and merge into the hash
0
+ # Hash to merge the converted element into.
0
+ # XML element to merge into hash
0
+ def self.merge_element!(hash, element)
0
+ merge!(hash, element.name, collapse(element))
0
+ # Actually converts an XML document element into a data structure.
0
+ # The document element to be collapsed.
0
+ def self.collapse(element)
0
+ hash = get_attributes(element)
0
+ if element.has_elements?
0
+ element.each_element {|child| merge_element!(hash, child) }
0
+ merge_texts!(hash, element) unless empty_content?(element)
0
+ re
turn merge_texts!(hash, element)0
- def self.xml_in_string(string, options = nil)
0
- new.xml_in_string(string, options)
0
+ # Merge all the texts of an element into the hash
0
+ # Hash to add the converted emement to.
0
+ # XML element whose texts are to me merged into the hash
0
+ def self.merge_texts!(hash, element)
0
+ unless element.has_text?
0
+ # must use value to prevent double-escaping
0
+ text_values = element.texts.map {|t| t.value }
0
+ merge!(hash, CONTENT_KEY, text_values.join)
0
+ # Adds a new key/value pair to an existing Hash. If the key to be added
0
+ # already exists and the existing value associated with key is not
0
+ # an Array, it will be wrapped in an Array. Then the new value is
0
+ # appended to that Array.
0
+ # Hash to add key/value pair to.
0
+ # Value to be associated with key.
0
+ def self.merge!(hash, key, value)
0
+ if hash[key].instance_of?(Array)
0
+ hash[key] = [ hash[key], value ]
0
+ elsif value.instance_of?(Array)
0
+ # Converts the attributes array of an XML element into a hash.
0
+ # Returns an empty Hash if node has no attributes.
0
+ # XML element to extract attributes from.
0
+ def self.get_attributes(element)
0
+ element.attributes.each { |n,v| attributes[n] = v }
0
+ # Determines if a document element has text content
0
+ # XML element to be checked.
0
+ def self.empty_content?(element)
0
+ element.texts.join.strip.empty?
0
@@ -166,15 +259,7 @@ module ActiveSupport #:nodoc:
0
- # TODO: Refactor this into something much cleaner that doesn't rely on XmlSimple
0
- typecast_xml_value(undasherize_keys(XmlSimple.xml_in_string(xml,
0
- 'forcearray' => false,
0
- 'forcecontent' => true,
0
- 'contentkey' => '__content__')
0
+ typecast_xml_value(undasherize_keys(XmlMini.parse(xml)))
Too bad you’re still using REXML :(
ditto
We’d be more than happy to take patches to use alternative reliable parsers guys.