×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Gerrit Viljoen
Added: Sep 22, 2014 1:08 PM
Modified: Sep 22, 2014 1:16 PM
Views: 23
Encode text for XML.
  1.     public static String encodeXML(CharSequence text) {
  2.         final String string = (text == null ? null : text.toString());
  3.         if (string == null) {
  4.             return null;
  5.         } else {
  6.             return string
  7.                     .replace("&", "&")
  8.                     .replace("\"", """)
  9.                     .replace("'", "'")
  10.                     .replace("<", "&lt;")
  11.                     .replace(">", "&gt;");
  12.         }
  13.     }