Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
XML file using Nokogiri gem
2 Posts
XML file using Nokogiri gem

Hello friends,

Can you guys give me some idea about how to Create XML file using Nokogiri gem.

I believe you have to use the "Nokogiri::XML::Builder":http://nokogiri.rubyforge.org/nokogiri/Nokogiri/XML/Builder.html class.

builder = Nokogiri::XML::Builder.new do |xml|
  xml.root {
    xml.products {
      xml.widget {
        xml.id_ "10"
        xml.name "Awesome widget"
puts builder.to_xml

Then you can write the XML to file as you normally would in Ruby:

f = File.new("products.xml", "w")

p. And that's that

2 Posts
Login to add your message