Saving generated XML to a file

1,291 views
Skip to first unread message

bobthabuilda

unread,
Dec 3, 2009, 1:23:18 PM12/3/09
to nokogiri-talk
i'm attempting to generate new XML documents and save them to a file
in a custom location. however, after reading the documentation on the
XML builder (http://nokogiri.org/Nokogiri/XML/Builder.html) i see
nothing mentioned in the explanations or the code that talks about XML
file creation and saving. i am fairly new to rails and ruby so i may
be missing something.

how can i save the generated XML document to a file?

Iñaki Baz Castillo

unread,
Dec 3, 2009, 1:29:30 PM12/3/09
to nokogi...@googlegroups.com
XML::Documents inherits from XML::Node, and XML::Node includes the method
"to_xml" which converts the XML document/node to a String.
Later you can store the string in a file as usual.

--
Iñaki Baz Castillo <i...@aliax.net>

bobthabuilda

unread,
Dec 3, 2009, 1:41:58 PM12/3/09
to nokogiri-talk
just figured it out.

f = File.new('output.xml', 'w')
f.write(builder.to_xml)
f.close

i was not familiar with this. thanks.

Gregory Clarke

unread,
Dec 3, 2009, 2:07:32 PM12/3/09
to nokogi...@googlegroups.com
Here's how I save an XML file out of a rails app:

send_data xml_data, :filename => "data.xml", :disposition => 'attachment'

Hope that helps.

Greg
Reply all
Reply to author
Forward
0 new messages