public static String encodeAsXMLName ( String s ) { StringBuilder sb = new StringBuilder ( "_" ) ; for ( byte b : s . getBytes ( Charset . forName ( "UTF-8" ) ) ) { sb . append ( Integer . toHexString ( ( b >>> 4 ) & 0xF ) ) ; sb . append ( Integer . toHexString ( b & 0xF ) ) ; } return sb . toString ( ) ; }
This will encode the given string as a valid XML name . The format will be an initial underscore followed by the hexadecimal representation of the string .