Skip to content

Instantly share code, notes, and snippets.

@dineshprabu-freshdesk
Last active October 12, 2016 13:50
Show Gist options
  • Save dineshprabu-freshdesk/9c052b30ed6fdc6c541a039d0bcb1fc0 to your computer and use it in GitHub Desktop.
Save dineshprabu-freshdesk/9c052b30ed6fdc6c541a039d0bcb1fc0 to your computer and use it in GitHub Desktop.
Zendesk XML Splitter
require 'nokogiri' # gem install nokogiri
require 'ruby-cheerio' # gem install ruby-cheerio
#Specify the file name to be chunked here
xml_file = "tickets.xml";
#No of chunks to be made
no_of_chunks = 4
doc = Nokogiri::XML(File.open(xml_file));
tickets = doc.xpath("//ticket")
report_date = doc.xpath("//tickets/@report_date").text
p "No of tickets being chunked: #{tickets.length}"
no_of_nodes_per_file = tickets.length/no_of_chunks + tickets.length%no_of_chunks;
new_chunk_flag = 0
chunk_number = 0
chunk_file_name = "tickets_#{chunk_number}.xml"
chunk_file = open(chunk_file_name, 'w')
chunk_file.write("<?xml version='1.0' encoding='UTF-8'?><tickets report_date=\"#{report_date}\">");
tickets.each_with_index do |ticket|
if( new_chunk_flag == no_of_nodes_per_file)
chunk_file.write('</tickets>')
chunk_number = chunk_number+1;
chunk_file_name = "tickets_#{chunk_number}.xml"
chunk_file = open(chunk_file_name, 'w')
chunk_file.write("<?xml version='1.0' encoding='UTF-8'?><tickets report_date=\"#{report_date}\">");
new_chunk_flag = 0
end
p ticket.to_s
p chunk_file_name
chunk_file.write(ticket.to_s)
new_chunk_flag = new_chunk_flag+1
end
chunk_file.write('</tickets>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment