Nokogiri is designed to build in memory because you build a DOM and it converts it to XML on the fly. It's easy to use, but there are trade-offs, and doing it in memory is one of them.
You might want to look into using Erubis to generate the XML. Rather than gather all the data before processing and keeping the logic in a controller, like we'd do with Rails, to save memory you can put your logic in the template and have it iterate over your data, which should help with the resource demands.
If you need the XML in a file you might need to do that using redirection:
erubis options templatefile.erb > xmlfile
This is a very simple example, but it shows you could easily define a template to generate XML:
<%
asdf = (1..5).to_a
%>
<xml>
<element>
<% asdf.each do |i| %>
<subelement><%= i %></subelement>
<% end %>
</element>
</xml>
which, when I call erubis test.erb
outputs:
<xml>
<element>
<subelement>1</subelement>
<subelement>2</subelement>
<subelement>3</subelement>
<subelement>4</subelement>
<subelement>5</subelement>
</element>
</xml>
EDIT:
The string concatenation was taking forever...
Yes, it can simply because of garbage collection. You don't show any code example of how you're building your strings, but Ruby works better when you use <<
to append one string to another than when using +
.
It also might work better to not try to keep everything in a string, but instead to write it immediately to disk, appending to an open file as you go.
Again, without code examples I'm shooting in the dark about what you might be doing or why things run slow.