Here you can find the source of xmlEncode(String text)
public static String xmlEncode(String text)
//package com.java2s; /*//from w ww .j a va 2 s. c om * Copyright (C) 2005-2007 Oleh Hapon ohapon@users.sourceforge.net * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * Oleh Hapon * Kyiv, UKRAINE * ohapon@users.sourceforge.net */ public class Main { public static String xmlEncode(String text) { int length = text.length(); if (text != null && length > 0) { StringBuffer ret = new StringBuffer(length * 12 / 10); int last = 0; for (int i = 0; i < length; i++) { char c = text.charAt(i); switch (c) { //case ' ' : ret.append(" "); break; case '&': if (last < i) { ret.append(text.substring(last, i)); } last = i + 1; ret.append("&"); break; case '>': if (last < i) { ret.append(text.substring(last, i)); } last = i + 1; ret.append(">"); break; case '<': if (last < i) { ret.append(text.substring(last, i)); } last = i + 1; ret.append("<"); break; case '\"': if (last < i) { ret.append(text.substring(last, i)); } last = i + 1; ret.append("""); break; case '\'': if (last < i) { ret.append(text.substring(last, i)); } last = i + 1; ret.append("'"); break; default: break; } } if (last < length) { ret.append(text.substring(last)); } return ret.toString(); } return text; } }