private static String getEncoding ( String text ) { String result = "UTF-8" ; //默认编码格式 String xml = text . trim ( ) ; if ( xml . startsWith ( "<?xml" ) ) { int end = xml . indexOf ( "?>" ) ; String sub = xml . substring ( 0 , end ) ; StringTokenizer tokens = new StringTokenizer ( sub , " =\"\'" ) ; while ( tokens . hasMoreTokens ( ) ) { String token = tokens . nextToken ( ) ; if ( "encoding" . equals ( token ) ) { if ( tokens . hasMoreTokens ( ) ) { result = tokens . nextToken ( ) ; } break ; } } } return result ; }
Gets the encoding pattern from given XML file .