Java tutorial
//package com.java2s; import java.io.UnsupportedEncodingException; public class Main { /** * Gets the encoding of xml byte array. * * @param xmlData * the xml data * @return the encoding of xml byte array * @throws Exception * the exception */ public static String getEncodingOfXmlByteArray(byte[] xmlData) throws Exception { String encodingAttributeName = "ENCODING"; try { String first50CharactersInUtf8UpperCase = new String(xmlData, "UTF-8").substring(0, 50).toUpperCase(); if (first50CharactersInUtf8UpperCase.contains(encodingAttributeName)) { int encodingstart = first50CharactersInUtf8UpperCase.indexOf(encodingAttributeName) + encodingAttributeName.length(); int contentStartEinfach = first50CharactersInUtf8UpperCase.indexOf("'", encodingstart); int contentStartDoppelt = first50CharactersInUtf8UpperCase.indexOf("\"", encodingstart); int contentStart = Math.min(contentStartEinfach, contentStartDoppelt); if (contentStartEinfach < 0) { contentStart = contentStartDoppelt; } if (contentStart < 0) { throw new Exception("XmlByteArray-Encoding nicht ermittelbar"); } contentStart = contentStart + 1; int contentEndSingle = first50CharactersInUtf8UpperCase.indexOf("'", contentStart); int contentEndDouble = first50CharactersInUtf8UpperCase.indexOf("\"", contentStart); int contentEnd = Math.min(contentEndSingle, contentEndDouble); if (contentEndSingle < 0) { contentEnd = contentEndDouble; } if (contentEnd < 0) { throw new Exception("XmlByteArray-Encoding nicht ermittelbar"); } String encodingString = first50CharactersInUtf8UpperCase.substring(contentStart, contentEnd); return encodingString; } else { throw new Exception("XmlByteArray-Encoding nicht ermittelbar"); } } catch (UnsupportedEncodingException e) { throw new Exception("XmlByteArray-Encoding nicht ermittelbar"); } } }