Entity Substituter
require 'delegate'
require 'rexml/text'
require 'rexml/document'
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))
end
end
str = %{<?xml version="1.0"?>
<!DOCTYPE doc [
<!ENTITY product 'Stargaze'>
<!ENTITY version '2.3'>
]>
<doc>
&product; v&version;
</doc>}
doc = REXML::Document.new str
output = EntitySubstituter.new($stdout, doc)
doc.write(output)
Related examples in the same category