Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module XmlConvertor
- require 'ox'
- # Class for parsing the XML to Hash Objects and backwards.
- class XMLtoHash
- # Convert XML input to Hash object for easy handeling within the application
- # @param xml [String] The XML formated String you want to convert from
- # @return [Hash] XML input in a Hash object
- def XMLtoHash(xml)
- hash = Ox.load(xml, mode: :hash)
- return hash
- end
- # Convert Hash to valid XML, The hash will be converted recusive
- # @param hash [Hash] Hash object used as input
- # @param parent [Object] Parent element for sub element from hash. (used for recursion)
- # @return [String] XML valid string
- def HashtoXML(hash, parent = nil)
- # Check if parent is set else create the start element
- if parent.nil?
- parent = Ox::Document.new(:version => '1.0')
- end
- # Start looping through the hash elements and create elements for the keys
- # assign the new element to the parent or asssing a value to the element
- hash.each do |key, value|
- if value.is_a?(Hash)
- key = Ox::Element.new(key)
- parent << key
- self.HashtoXML(value, key)
- else
- key = Ox::Element.new(key)
- parent << key
- key << value
- end
- end
- return Ox.dump(parent)
- end
- end
- end
Add Comment
Please, Sign In to add comment