Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Builder - XML output is encoding HTML entities

I have a small ruby script which uses Builder.

require 'rubygems'
require 'builder'

content = <<eos
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em>
eos

xml = Builder::XmlMarkup.new
  xml.instruct! :xml, :version => '1.0'
  xml.book :id => 1.0 do
    xml.keyPic "keyPic1.jpg"
    xml.parts do
      xml.part :partId => "1", :name => "name" do
        xml.chapter :title => "title", :subtitle => "subtitle" do
          xml.text content
        end
      end
    end
  end

p xml

When running from the CLI (Cygwin), I get the following:

<?xml version="1.0" encoding="UTF-8"?>
<book id="1.0">
  <keyPic>keyPic1.jpg</keyPic>
    <parts>
      <part partId="1" name="name">
        <chapter title="title" subtitle="subtitle">
          <text>
          SOME TEXT, GOES TO UPPERCASE
          other text
          &lt;em&gt;italics&lt;em&gt;
          </text>
        </chapter>
      </part>
    </parts>
</book><inspect/>

However, the output I would like between is:

<text>
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em/>
</text>

I have tried using the htmlentities gem 'decoding' the content but to no avail.

like image 287
agnitio Avatar asked Mar 23 '11 11:03

agnitio


1 Answers

Use the << operation to insert your text without modification.

xml.text do |t|
  t << content
end
like image 56
Viacheslav Molokov Avatar answered Oct 01 '22 11:10

Viacheslav Molokov