How to pretty-print XML with Nokogiri

If you print an XML document after adding nodes to it with Nokogiri, the additions may not print at the correct level of indentation. To correct the problem, tell Nokogiri that whitespace between nodes is not meaningful when you first read in the XML document:

doc = Nokogiri.XML(string) do |config|

Later, print the XML document, passing :indent as an option to to_xml:

puts doc.to_xml(:indent => 2)