Java tutorial
//package com.java2s; public class Main { /** Replace critical characters by XML entities. */ static String quoteCharacterData(char[] ch, int start, int length) { StringBuilder sb = new StringBuilder(); for (int i = start; i < start + length; i++) { char c; switch (c = ch[i]) { case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '&': sb.append("&"); break; default: sb.append(c); break; } } return sb.toString(); } /** Replace critical characters by XML entities. */ static String quoteCharacterData(String s) { return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">"); } }