-
-
Save anonymous/e50c397f09df08e88f8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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