Character encoding problem

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/). 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

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/). 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs