一目了然 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 ">" #=> ">" 1.9.2p320 :004 > require 'cgi' # => false 1.9.2p320 :005 > CGI.unescapeHTML """ # => "\"" 1.9.2p320 :006 > helper.send :raw, "<" # => "<" 1.9.2p320 :007 > helper.send :raw, "</p>" # => "</p>" 1.9.2p320 :008 > helper.send :h, "</p>" # => "</p>"
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 <em>italics<em> </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> 人发表留言,猛击->><strong>这里</strong><<-参与讨论</a>
</span>
<br><br><br>
ITeye推荐