5

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.

agnitio
  • 137
  • 1
  • 10

1 Answers1

6

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

xml.text do |t|
  t << content
end
Viacheslav Molokov
  • 2,534
  • 21
  • 20
  • ut, obviosly, you have to be careful that your content is valid in the context in which is is included. If a user enters "This is bad", do you successfully catch it before it gets to the XML stage, or are you okay with getting invalid XML? – Thomas Andrews Mar 23 '11 at 15:36
  • Yes, you should check it with something like HTML/XML validator. This is very dangerous behaviour in 'wild' environment. – Viacheslav Molokov Mar 23 '11 at 15:52