public static String encodeXmlAttribute ( String str ) { if ( str == null ) return null ; int len = str . length ( ) ; if ( len == 0 ) return str ; StringBuffer encoded = new StringBuffer ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = str . charAt ( i ) ; if ( c == ' ' ) encoded . append ( "&lt;" ) ; else if ( c == ' ' ) encoded . append ( "&quot;" ) ; else if ( c == ' ' ) encoded . append ( "&gt;" ) ; else if ( c == ' ' ) encoded . append ( "&apos;" ) ; else if ( c == ' ' ) encoded . append ( "&amp;" ) ; else encoded . append ( c ) ; } return encoded . toString ( ) ; }
Encodes a String with reserved XML characters into a valid xml string for attributes .