一目了然 rails html xml encode decode

2013-05-04 07:37

一目了然 rails html xml encode decode

by

at 2013-05-03 23:37:38

original http://hlee.iteye.com/blog/1859583

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

          <br><br>
          <span style="color:red">
            <a href="http://hlee.iteye.com/blog/1859583#comments" style="color:red">已有 <strong>0</strong> 人发表留言,猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br><br><br>

ITeye推荐