Last active
April 20, 2016 17:53
-
-
Save eduardocl/6b7fa387f0ff1460232dff32749effe3 to your computer and use it in GitHub Desktop.
Saving documents in xml
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
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