# Character encoding problem

**URL:** <https://www.ruby-forum.com/t/character-encoding-problem/193831>\
**Category:** Ruby\
**Created:** [August 25, 2010, 9:08am UTC](https://www.ruby-forum.com/t/character-encoding-problem/193831 "2010-08-25T09:08:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dgaj](https://www.ruby-forum.com/u/dgaj)\
**Post date:** [August 25, 2010, 9:08am UTC](https://www.ruby-forum.com/t/character-encoding-problem/193831/1 "2010-08-25T09:08:06Z")

</div>

I am getting an encoding problem when trying to write an xml file out  
after it generated by a StringTemplater  
([http://github.com/starapor/slippers/tree/](http://github.com/starapor/slippers/tree/)). I am using the templater to  
generate an xml file from within Cucumber. The template looks fine and  
produces xml correctly but when you use the string from the  
engine.render(Slippers::BindingWrapper.new(binding)).to\_str method and  
pass it to a new rexml\document or File.new the generated file is  
unreadable.

I have included the problem file on gist, link below and the example is  
called encoding\_problem.rb.

git clone git://gist.github.com/546222.git gist-546222

---

<div class="post-metadata">

**Author:** ![dgaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dgaj](https://www.ruby-forum.com/u/dgaj)\
**Post date:** [August 25, 2010, 11:57am UTC](https://www.ruby-forum.com/t/character-encoding-problem/193831/2 "2010-08-25T11:57:35Z")

</div>

Dave Green wrote:

> I am getting an encoding problem when trying to write an xml file out  
> after it generated by a StringTemplater  
> ([http://github.com/starapor/slippers/tree/](http://github.com/starapor/slippers/tree/)). I am using the templater to  
> generate an xml file from within Cucumber. The template looks fine and  
> produces xml correctly but when you use the string from the  
> engine.render(Slippers::BindingWrapper.new(binding)).to\_str method and  
> pass it to a new rexml\document or File.new the generated file is  
> unreadable.
> 
> I have included the problem file on gist, link below and the example is  
> called encoding\_problem.rb.
> 
> git clone git://gist.github.com/546222.git gist-546222

I git cloned the file and looked through to see if i could find the  
problem. Seems more than just an encoding problem from when I worked on  
it.  
In both encoding\_problem.rb and test\_templater.rb, I had to change  
Slippers to lowercase slippers when requiring it.  
Also, the encoding problem appears to be because of how Document.new  
works. In the xml\_template.st, once I changed it from “utf-16” to  
“utf-8”, it started parsing correctly, but had other errors. I’m  
guessing this is because the string uses 1 byte per character, but  
utf-16 gave the Document.new the impression that each character used 2  
bytes.  
Once I tried running the encoding\_problem.rb again, I eventually  
realized from the error that a tag in items.st wasn’t closed properly.  
The ending price tag is missing a slash. After all this was done, it’s  
output was:

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

\<response\_id\>1000\</response\_id\>  
\<number\_of\_products\>2\</number\_of\_products\>  
  
\<stock\_code\>ABC001\</stock\_code\>  
An item description  
100  
  
  
\<stock\_code\>ABC002\</stock\_code\>  
Another item  
150

I hope this was helpful.  
My ruby is 1.8.7

---

<div class="post-metadata">

**Author:** ![dgaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dgaj](https://www.ruby-forum.com/u/dgaj)\
**Post date:** [August 25, 2010, 12:25pm UTC](https://www.ruby-forum.com/t/character-encoding-problem/193831/3 "2010-08-25T12:25:27Z")

</div>

That works great, thank you for all your effort in looking into this,  
much appreciated.
