Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2014 07:23
Show Gist options
  • Save anonymous/e50c397f09df08e88f8f to your computer and use it in GitHub Desktop.
Save anonymous/e50c397f09df08e88f8f to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'active_support/core_ext' #in-built with standard Ruby on Rails libraries
require 'json' #same as above
require 'json/stream' #command line: gem install json-stream
require 'open-uri' #open the stream
def convert_to_XML(input_file, output_file, type_of_feed)
stream = File.open input_file #takes the input from the input_file
json = JSON::Stream::Parser.parse(stream) #special magic to make the JSON stream instead of 1 big file into memory
xml = json.to_xml(:root => type_of_feed) #resultant XML file to write out
to_file = File.open(output_file, 'w+') {
|file|
file.write(xml)
}
end
def from_JSON_to_XML(input_url, output_file, type_of_feed)
stream = open(input_url).read #takes the input from a URL stream
json = JSON::Stream::Parser.parse(stream) #special magic to make JSON stream instead of 1 big net feed
xml = json.to_xml(:root => type_of_feed)
to_file = File.open(output_file, 'w+') {
|file|
file.write(xml)
}
end
convert_to_XML('sample-soccer.json', 'soccer.xml','soccer')
from_JSON_to_XML('http://www.example.com/api/feed/competitors?event_id=2319508', 'competitors.xml', 'horsies')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment