Here you can find the source of toXMLString(String javaString)
public static String toXMLString(String javaString)
//package com.java2s; /**/*from ww w .j a v a2 s . com*/ * Copyright 2004-2014 Riccardo Solmi. All rights reserved. * This file is part of the Whole Platform. * * The Whole Platform 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 3 of the License, or * (at your option) any later version. * * The Whole Platform 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 the Whole Platform. If not, see <http://www.gnu.org/licenses/>. */ public class Main { public static String toXMLString(String javaString) { char[] charArray = javaString.toCharArray(); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < charArray.length; i++) { final char ch = charArray[i]; switch (ch) { case ' ': buffer.append(' '); break; case '<': buffer.append("<"); break; case '>': buffer.append(">"); break; case '\'': buffer.append("'"); break; case '"': buffer.append("""); break; case '&': buffer.append("&"); break; case '\b': buffer.append(""); break; case '\t': buffer.append("	"); break; case '\n': buffer.append(" "); break; case '\f': buffer.append(""); break; case '\r': buffer.append(" "); break; default: if (Character.isISOControl(ch) || Character.isWhitespace(ch)) { buffer.append("&#x"); buffer.append(charToHex(ch)); buffer.append(';'); } else buffer.append(ch); break; } } return buffer.toString(); } public static boolean isWhitespace(CharSequence cs) { int count = 0, length = cs.length(); while (count < length && Character.isWhitespace(cs.charAt(count))) count++; return count == length; } public static String charToHex(char c) { // Returns hex String representation of char c byte hi = (byte) (c >>> 8); byte lo = (byte) (c & 0xff); return byteToHex(hi) + byteToHex(lo); } public static String byteToHex(byte b) { // Returns hex String representation of byte b char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] }; return new String(array); } }