If I have a string which may contain any characters (including ‘/’, ‘&’,etc…) how do convert it safely into XML that can be stored like this:
<myelement>mystring</myelement>
Does it need to be CDATA, or can I easily convert it using a ruby function?
In Ruby 1.9.2 to escape XML special characters in Strings, use the ‘encode’ method.
Example, if you have:
For XML attributes use:
Generates:
For XML text use:
Generates: