Java tutorial
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class Main { public static boolean isValidXmlFile(final File file) { boolean result; try { final InputStream input = new FileInputStream(file.getPath()); final byte[] header = new byte[32]; input.read(header, 0, 32); String headerStr = new String(header, "ASCII"); String XML_REGEX = "^\\s*<(\\?|!(--)?)?\\s*\\w+.*"; result = headerStr.matches(XML_REGEX); try { input.close(); } catch (Exception e) { // ignore this exception } } catch (Exception e) { result = false; } return result; } }