Java stax:3 字节 UTF-8 序列的无效字节 2

Java stax: Invalid byte 2 of 3-byte UTF-8 sequence

我正在尝试使用 stax 解析 xml,但我得到的错误是:

javax.xml.stream.XMLStreamException: ParseError at [row,col]:[8,64]
Message: Invalid byte 2 of 3-byte UTF-8 sequence.

我已经尝试查找它但找不到解决方案。我必须解析的代码是:

public List<Vild> getVildData(File file){
    XMLInputFactory factory = XMLInputFactory.newFactory();
    try {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(Files.readAllBytes(file.toPath()));
        XMLStreamReader reader = factory.createXMLStreamReader(byteArrayInputStream);
        List<Vild> vild = saveVild(reader);
        reader.close();
        return vild;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    return Collections.emptyList();
}
private List<Vild> saveVild(XMLStreamReader streamReader) {
    List<Vild> vildList = new ArrayList<>();
    try{
        Vild vild = new Vild();
        while (streamReader.hasNext()) {
            streamReader.next();
            //Creating list with data
        }
    }catch(XMLStreamException | IllegalStateException ex) {
        ex.printStackTrace();
    }
    return Collections.emptyList();
}

我已经尝试过在网上找到的以下方法:

XMLStreamReader reader = factory.createXMLStreamReader(byteArrayInputStream,"UTF-8");

但这没有用。有人知道这个问题的解决方案吗?

您的 XML 文件未使用 UTF-8 编码。尝试找出编码是什么。

例如,如果编码结果是 "latín 1",则在创建 xml reader:

时使用它
XMLStreamReader reader = factory.createXMLStreamReader(byteArrayInputStream,"ISO8859-1")