Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Simple Particle Engine for a shooter game
Snake / Nibbles clone in C and Ncurses
Please improve
Parsing of XML data has high CPU usage
Convert simple Javascript to jQuery plugin
Active Record getting unique records
List the files in a directory without the directory name or the extension
clean the code
ohs system, recruitment software, hr software, oh&s software, human resources software, ohs software
Array parsing in a block
Popular
Parsing of XML data has high CPU usage
Please improve
Snake / Nibbles clone in C and Ncurses
List the files in a directory without the directory name or the extension
Convert simple Javascript to jQuery plugin
Simple Particle Engine for a shooter game
Active Record getting unique records
Breadth first cartesian product iterator
php refactoring
first BST
Pastable version of
Create a Hash from XML using Hpricot
<pre class='prettyprint' language='ruby'>require 'hpricot' class Parser # converts a given soap response xml into a hash. def self.soap_response_to_hash(xml) xml = prepare_xml(xml) doc = Hpricot.XML(xml) root = doc.at("//return") xml_node_to_hash(root) end private # converts xml nodes into a hash. recursively calls # itself in case of nested xml nodes. def self.xml_node_to_hash(node) this_node = {} node.each_child do |child| if child.children.nil? key, value = child.name, nil elsif child.children.size == 1 && child.children.first.text? key, value = child.name, string_to_bool?(child.children.first.raw_string) else key, value = child.name, xml_node_to_hash(child) end current = this_node[key] case current when Array: this_node[key] << value when nil this_node[key] = value else this_node[key] = [current.dup, value] end end this_node end # removes line breaks and whitespace between xml nodes. def self.prepare_xml(xml) xml = xml.gsub(/\n+/, "") xml = xml.gsub(/(>)\s*(<)/, '\1\2') end # converts "true" and "false" to boolean values or # returns the given string otherwise. def self.string_to_bool?(string) return true if string == "true" return false if string == "false" string end end xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:authenticateResponse xmlns:ns2="http://v1_0.ws.some.namespace/"> <return> <authenticationValue> <orderToken>dfgdfg-4356345-sdfsdf-2345234-234</orderToken> <tokenHash>000111DDD888444777NNNZZZ555000</tokenHash> <client>dude</client> </authenticationValue> <success>true</success> </return> </ns2:authenticateResponse> </soap:Body> </soap:Envelope>' hash = Parser::soap_response_to_hash(xml)</pre> <a href="http://www.refactormycode.com/codes/932-create-a-hash-from-xml-using-hpricot" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>