@ Nonnull public static String escapeXML ( CharSequence s ) { // double quote -- quot // ampersand -- amp // less than -- lt // greater than -- gt // apostrophe -- apos StringBuilder sb = new StringBuilder ( s . length ( ) * 2 ) ; for ( int i = 0 ; i < s . length ( ) ; ) { int codePoint = Character . codePointAt ( s , i ) ; if ( codePoint == ' ' ) { sb . append ( LT ) ; } else if ( codePoint == ' ' ) { sb . append ( GT ) ; } else if ( codePoint == ' ' ) { sb . append ( QUOT ) ; } else if ( codePoint == ' ' ) { sb . append ( AMP ) ; } else if ( codePoint == ' ' ) { sb . append ( APOS ) ; } else { sb . appendCodePoint ( codePoint ) ; } i += Character . charCount ( codePoint ) ; } return sb . toString ( ) ; }
Escapes a character sequence so that it is valid XML .