一目了然 rails html xml encode decode

1.9.2p320 :001 > require 'htmlentities'
1.9.2p320 :002 > c = HTMLEntities.new
 #=> #<HTMLEntities:0x007fef0a5b0468 @flavor="xhtml1">
1.9.2p320 :003 > c.decode "&gt;"
 #=> ">"
1.9.2p320 :004 > require 'cgi'
# => false
1.9.2p320 :005 > CGI.unescapeHTML "&quot;"
# => "\""
1.9.2p320 :006 > helper.send :raw, "&lt;"
# => "&lt;"
1.9.2p320 :007 > helper.send :raw, "</p>"
# => "</p>"
1.9.2p320 :008 > helper.send :h, "</p>"
# => "&lt;/p&gt;"




xml稍微特别处理
require 'rubygems'
require 'builder'

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


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


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



使用<< 操作符来添加不修改内容




xml.text do |t|
  t << content
end

你可能感兴趣的:(html,xml,Rails)