Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Character encoding problem

Reply
Thread Tools

Character encoding problem

 
 
Dave Green
Guest
Posts: n/a
 
      08-25-2010
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Kc Co
Guest
Posts: n/a
 
      08-25-2010
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'?>
<SearchResponse xmlnssi='http://www.w3.org/2001/XMLSchema-instance'
xmlnssd='http://www.w3.org/2001/XMLSchema'>
<response_id>1000</response_id>
<number_of_products>2</number_of_products>
<items>
<stock_code>ABC001</stock_code>
<description>An item description</description>
<price>100</price>
</items>
<items>
<stock_code>ABC002</stock_code>
<description>Another item</description>
<price>150</price>
</items>

</SearchResponse>

I hope this was helpful.
My ruby is 1.8.7
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Dave Green
Guest
Posts: n/a
 
      08-25-2010
That works great, thank you for all your effort in looking into this,
much appreciated.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with character encoding in commandline gialloporpora Python 1 10-15-2009 06:56 PM
character encoding problem bj Java 2 06-15-2007 09:58 AM
Character encoding problem using java and MySql 5.0 Dhananjay Java 8 05-10-2006 06:03 AM
character encoding +missing character sequence raavi Java 2 03-02-2006 05:01 AM
Character encoding problem? =?Utf-8?B?VGhvbWFzIEthcmxzc29u?= ASP .Net 0 02-02-2004 09:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57