Skip to content

Instantly share code, notes, and snippets.

@eduardocl
Last active April 20, 2016 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardocl/6b7fa387f0ff1460232dff32749effe3 to your computer and use it in GitHub Desktop.
Save eduardocl/6b7fa387f0ff1460232dff32749effe3 to your computer and use it in GitHub Desktop.
Saving documents in xml
desc "Salva arquivos em xml"
task :export => :environment do
require 'builder'
docs = Document.all
index = 1
docsarray = []
docs.each do |doc|
docsarray.push(doc)
if docsarray.size % 1000 == 0
save_xml("documents-rkb-" + index.to_s, docsarray)
docsarray = []
index = index + 1
end
end
if docsarray.size > 0
save_xml("documents-rkb-" + index.to_s, docsarray)
end
end
def save_xml(filename, docs)
xml = Builder::XmlMarkup.new( :indent => 2 )
xml.instruct! :xml, :encoding => "UTF-8"
xml.documents do |documents|
docs.each do |doc|
documents.document do |document|
document.title doc.title
document.publisher doc.publisher
document.description doc.description
document.year doc.year
document.path doc.attachment.path
document.type doc.attachment_content_type
document.size doc.attachment_file_size
document.tags do |tag|
doc.tags.each do |t|
tag.tag t.name
end
end
document.authors do |author|
doc.authors do |a|
author.author a.name
end
end
end
end
end
filexml = File.new(filename + ".xml", "w", :encondig => "utf-8")
filexml.write(xml.target!)
filexml.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment