code: public static String escape(String value)
      {
          String search = "&<>";
          String[] replace = {"&amp;", "&lt;", "&gt;"};
  
          StringBuilder buf = new StringBuilder();
  
          for (int i = 0; i < value.length(); i++)
          {
              char c = value.charAt(i);
              int pos = search.indexOf(c);
              if (pos < 0)
                  buf.append(c);
              else
                  buf.append(replace[pos]);
          }
  
          return buf.toString();
      }
docs: Convert special characters to entities for XML output