Java tutorial
//package com.java2s; /*-------------------------------------------------------------------------- * Copyright (c) 2004, 2006 OpenMethods, LLC * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Trip Gilman (OpenMethods), Lonnie G. Pryor (OpenMethods), * Vincent Pruitt (OpenMethods) * * T.D. Barnes (OpenMethods) - initial API and implementation -------------------------------------------------------------------------*/ public class Main { /** * Encodes the given string so that it conforms to the XML 1.0 specification * for CDATA and TEXT nodes. * * @param toEncode The string to encode * * @return the encoded string */ public static String encodeText(String toEncode) { StringBuffer buffer = new StringBuffer(); char[] cs = toEncode.toCharArray(); for (int i = 0; i < cs.length; i++) { switch (cs[i]) { case '&': buffer.append("&"); break; case '<': buffer.append("<"); break; case '>': buffer.append(">"); break; default: buffer.append(cs[i]); } } return buffer.toString(); } }