Mega Code Archive

 
Categories / Ruby / XML
 

Assign new value to node

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 text_node = doc.root.children[0] text_node.value = "&product; v&version;" doc.write