Expanding a variable from / inside (XML) source file in Ruby
I want some "dynamic" XML source files. I want some of the element values ββto be dynamic or "expandable".
I am providing a value for this variable from another source.
@Name = 'freezingkiwis'
What I want to provide is something like this in an XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Contact>
<Name>#{@Name}</Name>
<Addresses />
<Phones />
</Contact>
When opening / reading a file (presumably File.new or File.read) I want to then "extend" the @Name variable I set earlier.
Is there something that will do this immediately for me, or will I have to parse this XML (perhaps now as REXML :: Document) and do it manually myself?
(maybe replace element value with this ...)
J
Consider using ERB , a Ruby templating engine used in Rails, for example. Then you can write any Ruby code, including variable substitution, between tags <% %>
. On the other hand, do not use this solution if the XML template itself is editable by users - it will lead to a serious security hole.