Created
October 3, 2012 16:10
-
-
Save coffeejunk/3827905 to your computer and use it in GitHub Desktop.
ruby string for xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "my" complicated <String>" /> | |
For XML text use: | |
"<node>#{my_string.encode(:xml => :text)}</node>" | |
Generates: | |
<node>this is "my" complicated <String></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