Skip to content

Instantly share code, notes, and snippets.

@coffeejunk
Created October 3, 2012 16:10
Show Gist options
  • Save coffeejunk/3827905 to your computer and use it in GitHub Desktop.
Save coffeejunk/3827905 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