Rectangle 27 0

ruby on rails Nokogiri XML file output is located where?


Dir.pwd
File.expand_path('xml.out')

Your file was written to xml.out. Where is this file? It depends on what the working directory was when you started your application, and whether your code or any library ever called Dir.chdir to change the working directory.

Note
Rectangle 27 0

ruby on rails Nokogiri XML file output is located where?


# open a file instance with path '/path/to/file.xml' in write mode (-> 'w')
File.open('/path/to/file.xml', 'w') do |file|
  # write the xml string generated above to the file
  file.write xml_string
end
builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
  xml.root do
    xml.products do
      xml.widget do
        xml.id_ "10"
        xml.name "Awesome widget"
      end
    end
  end
end
xml_string = builder.to_xml # => "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>\n  <products>\n    <widget>\n      <id>10</id>\n      <name>Awesome widget</name>\n    </widget>\n  </products>\n</root>\n"

An important note: you always have to make sure that you close all files that you opened if you no longer need them. File.open with a block (as used in my snipped above) will automatically close the file for you after the code in the block is executed...

First, you have to build your xml:

To generate an xml file you do not need Rails. Ruby (and Nokogiri or some oder builder) will suffice.

To save this string into a file, you use File.open and File#write:

You can get an xml string from this builder using builder.to_xml:

Note