I want some slightly ‘dynamic’ XML source files. I want some of the element values to be dynamic, or ‘expandable’.
I’m providing a value for this variable, from some other 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 the file (presumably File.new or File.read) I want to then ‘expand’ the @Name variable, set previously.
Is there something that will do this immediately for me, or am I going to have to parse this XML (possibly now as a REXML::Document) and do this manually myself?
(i.e. possibly replace the element value with this…)
J
Consider using ERB, a Ruby templating engine used for example in Rails. Then you can write any Ruby code, including variable substitution, between the
<% %>tags. On the other hand don’t use this solution if the XML template itself is editable by users – this will lead to a severe security hole.