⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 07 - substituting xml entities.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
require 'rexml/document'str = %{<?xml version="1.0"?><!DOCTYPE doc [ <!ENTITY product 'Stargaze'> <!ENTITY version '2.3'>]><doc> &product; v&version; is the most advanced astronomy product on the market.</doc>}doc = REXML::Document.new strdoc.root.children[0].value# => "\n Stargaze v2.3 is the most advanced astronomy product on the market.\n"doc.root.text# => "\n Stargaze v2.3 is the most advanced astronomy product on the market.\n"doc.root.children[0].to_s# => "\n &product; v&version; is the most advanced astronomy product on the market.\n"doc.root.write# <doc>#  &product; v&version; is the most advanced astronomy program on the market.# </doc>#---require 'delegate'require 'rexml/text'class EntitySubstituter < DelegateClass(IO)  def initialize(io, document, filter=nil)    @document = document    @filter = filter    super(io)  end    def <<(s)    super(REXML::Text::unnormalize(s, @document.doctype, @filter))  endendoutput = EntitySubstituter.new($stdout, doc)doc.write(output)# <?xml version='1.0'?><!DOCTYPE doc [# <!ENTITY product "Stargaze"># <!ENTITY version "2.3"># ]># <doc>#  Stargaze v2.3 is the most advanced astronomy product on the market.# </doc>#---text_node = doc.root.children[0]text_node.value = "&product; v&version; has a catalogue of 2.3 " +                  "million celestial objects."doc.write# <?xml version='1.0'?><!DOCTYPE doc [# <!ENTITY product "Stargaze"># <!ENTITY version "2.3"># ]># <doc>&product; v&version; has a catalogue of &version; million celestial objects.</doc>#---text_node.raw = truedoc.write# <?xml version='1.0'?><!DOCTYPE doc [# <!ENTITY product "Stargaze"># <!ENTITY version "2.3"># ]># <doc>&product; v&version; has a catalogue of 2.3 million celestial objects.</doc>text_node.value# => "Stargaze v2.3 has a catalogue of 2.3 million celestial objects."text_node.to_s# => "&product; v&version; has a catalogue of 2.3 million celestial objects."#---str = %{  <!DOCTYPE doc [ <!ENTITY year '2006'> ]>  <doc>&copy; &year; Komodo Dragon &amp; Bob Productions</doc>}doc = REXML::Document.new strtext_node = doc.root.children[0]text_node.value# => "&copy; 2006 Komodo Dragon & Bob Productions"text_node.to_s# => "&copy; &year; Komodo Dragon &amp; Bob Productions"#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -