public static String escapeXML(String rawContent, String charset, boolean isNoLines) {
		if (rawContent == null)
			return "";
		else {
			StringBuffer sb = new StringBuffer();

			for (int i = 0; i < rawContent.length(); i++) {
				char ch = rawContent.charAt(i);
				if (ch == '\'')
					sb.append("&#39;");
				else if (ch == '&')
					sb.append("&amp;");
				else if (ch == '"')
					sb.append("&quot;");
				else if (ch == '<')
					sb.append("&lt;");
				else if (ch == '>')
					sb.append("&gt;");
				else if (ch > '~' && charset != null && charSetImpliesAscii(charset)) 
					// TODO - why is hashcode the only way to get the unicode number for the character
					// in jre 5.0?
					sb.append("&#x"+Integer.toHexString(ch).toUpperCase()+";");
				else if (isNoLines) {
					if (ch == '\r')
						sb.append("&#xA;");
					else if (ch != '\n')
						sb.append(ch);
				}
				else
					sb.append(ch);
			}
			return sb.toString();
		}
	}
public static String escapeXML ( String rawContent , String charset , boolean isNoLines ) { if ( rawContent == null ) return "" ; else { StringBuffer sb = new StringBuffer ( ) ; for ( int i = 0 ; i < rawContent . length ( ) ; i ++ ) { char ch = rawContent . charAt ( i ) ; if ( ch == '\'' ) sb . append ( "&#39;" ) ; else if ( ch == '&' ) sb . append ( "&amp;" ) ; else if ( ch == '"' ) sb . append ( "&quot;" ) ; else if ( ch == '<' ) sb . append ( "&lt;" ) ; else if ( ch == '>' ) sb . append ( "&gt;" ) ; else if ( ch > '~' && charset != null && charSetImpliesAscii ( charset ) ) sb . append ( "&#x" + Integer . toHexString ( ch ) . toUpperCase ( ) + ";" ) ; else if ( isNoLines ) { if ( ch == '\r' ) sb . append ( "&#xA;" ) ; else if ( ch != '\n' ) sb . append ( ch ) ; } else sb . append ( ch ) ; } return sb . toString ( ) ; } }
Converts the raw characters to XML escape characters.

@param rawContent
@param charset Null when charset is not known, so we assume it's unicode
@param isNoLines
@return escape string