一目了然 rails html xml encode decode

阅读更多
1.9.2p320 :001 > require 'htmlentities'
1.9.2p320 :002 > c = HTMLEntities.new
 #=> #
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, "

" # => "

" 1.9.2p320 :008 > helper.send :h, "

" # => "</p>"



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

content = <italics


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:



  keyPic1.jpg
    
      
        
          
          SOME TEXT, GOES TO UPPERCASE
          other text
          <em>italics<em>
          
        
      
    




SOME TEXT, GOES TO UPPERCASE
other text
italics




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




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

  • 一目了然 rails html xml encode decode_第1张图片
  • 大小: 56.8 KB
  • 查看图片附件

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