Discussion Forums
Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
- Topic List
- Most Recent Posts
- Sign In for more options
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"
}
}
}
end
puts builder.to_xml
Then you can write the XML to file as you normally would in Ruby:
f = File.new("products.xml", "w")
f.write(builder.to_xml)
f.close
p. And that's that
2 Posts
