前言: 使用Jaxb反序列化xml,处理UTF-8 with bom的文件时报错。

问题: Content is not allowed in prolog.

解决方案:

public static Object convertXmlFileToObject(Class clazz, String xmlPath) {
        Object xmlObject = null;
        File file = new File(xmlPath);
        try (FileInputStream in = new FileInputStream(file);) {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();

            // 可检测多种类型,并剔除bom
            BOMInputStream bomIn = new BOMInputStream(in, false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE);
            String charset = "utf-8";
            // 若检测到bom,则使用bom对应的编码
            if (bomIn.hasBOM()) {
                charset = bomIn.getBOMCharsetName();
            }
            try (InputStreamReader reader = new InputStreamReader(bomIn, charset)) {
                xmlObject = unmarshaller.unmarshal(reader);
            }
        } catch (Exception e) {
            log.error("xml unmarshaller failed:{}", e);
        }
        return xmlObject;
    }