code: 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;
      }
docs: Gets the encoding pattern from given XML file.
  
  @param text the context of the XML file
  @return the encoding pattern string of given XML file