Escape HTML special characters from a String : HTML Parser « Network « Java Tutorial






public class Main {
  public static void main(String[] argv){
    System.out.println(escapeHTML("><"));
  }
  
  public static final String escapeHTML(String s) {
    StringBuffer sb = new StringBuffer();
    int n = s.length();
    for (int i = 0; i < n; i++) {
      char c = s.charAt(i);
      switch (c) {
      case '<':
        sb.append("&lt;");
        break;
      case '>':
        sb.append("&gt;");
        break;
      case '&':
        sb.append("&amp;");
        break;
      case '"':
        sb.append("&quot;");
        break;
      case '':
        sb.append("&agrave;");
        break;
      case '':
        sb.append("&Agrave;");
        break;
      case '':
        sb.append("&acirc;");
        break;
      case '':
        sb.append("&Acirc;");
        break;
      case '':
        sb.append("&auml;");
        break;
      case '':
        sb.append("&Auml;");
        break;
      case '':
        sb.append("&aring;");
        break;
      case '':
        sb.append("&Aring;");
        break;
      case '':
        sb.append("&aelig;");
        break;
      case '':
        sb.append("&AElig;");
        break;
      case '':
        sb.append("&ccedil;");
        break;
      case '':
        sb.append("&Ccedil;");
        break;
      case '':
        sb.append("&eacute;");
        break;
      case '':
        sb.append("&Eacute;");
        break;
      case '':
        sb.append("&egrave;");
        break;
      case '':
        sb.append("&Egrave;");
        break;
      case '':
        sb.append("&ecirc;");
        break;
      case '':
        sb.append("&Ecirc;");
        break;
      case '':
        sb.append("&euml;");
        break;
      case '':
        sb.append("&Euml;");
        break;
      case '':
        sb.append("&iuml;");
        break;
      case '':
        sb.append("&Iuml;");
        break;
      case '':
        sb.append("&ocirc;");
        break;
      case '':
        sb.append("&Ocirc;");
        break;
      case '':
        sb.append("&ouml;");
        break;
      case '':
        sb.append("&Ouml;");
        break;
      case '':
        sb.append("&oslash;");
        break;
      case '':
        sb.append("&Oslash;");
        break;
      case '':
        sb.append("&szlig;");
        break;
      case '':
        sb.append("&ugrave;");
        break;
      case '':
        sb.append("&Ugrave;");
        break;
      case '':
        sb.append("&ucirc;");
        break;
      case '':
        sb.append("&Ucirc;");
        break;
      case '':
        sb.append("&uuml;");
        break;
      case '':
        sb.append("&Uuml;");
        break;
      case '':
        sb.append("&reg;");
        break;
      case '':
        sb.append("&copy;");
        break;
      case '':
        sb.append("&euro;");
        break;
      case ' ':
        sb.append("&nbsp;");
        break;

      default:
        sb.append(c);
        break;
      }
    }
    return sb.toString();
  }
}
//&gt;&lt;








19.26.HTML Parser
19.26.1.Getting the Links in an HTML Document
19.26.2.Getting the Text in an HTML Document
19.26.3.Escape HTML special characters from a String
19.26.4.Using javax.swing.text.html.HTMLEditorKit to parse html document
19.26.5.Extract links from an HTML page
19.26.6.extends HTMLEditorKit.ParserCallback
19.26.7.HTML parser based on HTMLEditorKit.ParserCallback
19.26.8.Find and display hyperlinks contained within a web page
19.26.9.Get all hyper links from a web page
19.26.10.HTML Parser