Skip to content

Instantly share code, notes, and snippets.

@wallymathieu
Forked from coffeejunk/gist:3827905
Last active August 29, 2015 14:28
Show Gist options
  • Save wallymathieu/db05ab8dd46e0de05c11 to your computer and use it in GitHub Desktop.
Save wallymathieu/db05ab8dd46e0de05c11 to your computer and use it in GitHub Desktop.
ruby string for xml
In Ruby 1.9.2 to escape XML special characters in Strings, use the 'encode' method.
Example, if you have:
my_string = 'this is "my" complicated <String>'
For XML attributes use:
"<node attr=#{my_string.encode(:xml => :attr)} />"
Generates:
<node attr="this is &quot;my&quot; complicated &lt;String&gt;" />
For XML text use:
"<node>#{my_string.encode(:xml => :text)}</node>"
Generates:
<node>this is "my" complicated &lt;String&gt;</node>
source: http://stackoverflow.com/a/7573833/377593
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment