Java tutorial
//package com.java2s; import java.io.ByteArrayInputStream; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; public class Main { private static XMLInputFactory xif = XMLInputFactory.newInstance(); public static boolean hasValidXmlDeclaration(byte[] doc) throws Exception { try { XMLStreamReader r = xif.createXMLStreamReader(new ByteArrayInputStream(doc), null); if (!"UTF-8".equalsIgnoreCase(r.getEncoding())) return false; if (!"1.0".equals(r.getVersion())) return false; r.close(); return true; } catch (XMLStreamException e) { throw new Exception("Unable to parse xml", e); } } }