How do I get Nokogiri to add the right XML encoding?

Luc picture Luc · Dec 7, 2010 · Viewed 9.8k times · Source

I have created a xml doc with Nokogiri: Nokogiri::XML::Document

The header of my file is <?xml version="1.0"?> but I'd expect to have <?xml version="1.0" encoding="UTF-8"?>. Is there any options I could use so the encoding appears ?

Answer

LarsH picture LarsH · Dec 7, 2010

Are you using Nokogiri XML Builder? You can pass an encoding option to the new() method:

new(options = {})

Create a new Builder object. options are sent to the top level Document that is being built.

Building a document with a particular encoding for example:

  Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    ...
  end

Also this page says you can do the following (when not using Builder):

doc = Nokogiri.XML('<foo><bar /><foo>', nil, 'EUC-JP')

Presumably you could change 'EUC-JP' to 'UTF-8'.