# Libxml: trying to set encoding for a document

**URL:** <https://www.ruby-forum.com/t/libxml-trying-to-set-encoding-for-a-document/173506>\
**Category:** Ruby\
**Created:** [August 10, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/libxml-trying-to-set-encoding-for-a-document/173506 "2009-08-10T18:41:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diana\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@Diana\_J](https://www.ruby-forum.com/u/Diana_J)\
**Post date:** [August 10, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/libxml-trying-to-set-encoding-for-a-document/173506/1 "2009-08-10T18:41:32Z")

</div>

Hi all!

I have problems with setting encoding for an xml file. I want to produce  
an  
xml file which starts with

 \<?xml version="1.0" encoding="UTF-8"?\>

I tried the following code, but it gives initialized constant error when  
I  
try to set up document.encoding.

require ‘xml’

d = XML::Document.new()  
#d.encoding = XML::Encoding::UTF\_8  
#=\> uninitialized constant LibXML::XML::Encoding (NameError)  
d.root = XML::Node.new(‘node’)  
d.root \<\< “Some info”  
d.save(‘test.xml’, true)

produces:

\<?xml version="1.0"?\>

Some info

I tried to set encoding when saving the document, as shown in  
documentation,  
but without luck.

#d.save(‘test.xml’, :indent =\> true, :encoding =\> ‘UTF-8’)  
#=\>The second parameter (format) must be true or false (ArgumentError)  
#d.save(‘test.xml’, :indent =\> true, :encoding =\> XML::Encoding::UTF\_8)  
#=\> The second parameter (format) must be true or false (ArgumentError)

I checked ruby-libxml bug tracker and such bug has not been reported. So  
I  
am wondering if I am doing something wrong, or its a bug.

Thanks,

Diana

---

<div class="post-metadata">

**Author:** ![Diana\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@Diana\_J](https://www.ruby-forum.com/u/Diana_J)\
**Post date:** [August 11, 2009, 2:32am UTC](https://www.ruby-forum.com/t/libxml-trying-to-set-encoding-for-a-document/173506/2 "2009-08-11T02:32:13Z")

</div>

Diana J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have problems with setting encoding for an xml file. I want to produce an  
> xml file which starts with
> 
> \<?xml version="1.0" encoding="UTF-8"?\>

require ‘rubygems’  
require ‘libxml’  
doc = LibXML::XML::Document.new  
puts doc.to\_s #=\> \<?xml version="1.0" encoding="UTF-8"?\>

m.

---

<div class="post-metadata">

**Author:** ![Diana\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@Diana\_J](https://www.ruby-forum.com/u/Diana_J)\
**Post date:** [August 11, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/libxml-trying-to-set-encoding-for-a-document/173506/3 "2009-08-11T15:26:02Z")

</div>

- removed\_email\_address@domain.invalid (Matt N.) wrote:

> require ‘libxml’  
> doc = LibXML::XML::Document.new  
> puts doc.to\_s #=\> \<?xml version="1.0" encoding="UTF-8"?\>

You may also specify indentation and the explicit encoding  
with parameters to the to\_s method, like this:

doc.to\_s(:indent =\> true, :encoding =\> XML::Encoding::UTF\_8)

The documentation for XML::Document#to\_s is quite buggy and  
I’ve filed a ticket.
