简体   繁体   English

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

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

I am trying to parse a xml using stax but the error I get is:我正在尝试使用 stax 解析 xml,但我得到的错误是:

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

I have already tried to look it up but couldn't find a solution.我已经尝试查找它,但找不到解决方案。 The code I have to parse it is:我必须解析它的代码是:

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();
}

I have already tried the following that I found online:我已经尝试过我在网上找到的以下内容:

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

But that didn't work.但这没有用。 Does someone know a solution for this problem?有人知道这个问题的解决方案吗?

Your XML file is not encoded in UTF-8.您的 XML 文件不是以 UTF-8 编码的。 Try to find out what the encoding is.尝试找出编码是什么。

If the encoding turns out to be "latín 1" for example, use that when you create the xml reader:例如,如果编码结果是“latín 1”,则在创建 xml 阅读器时使用它:

XMLStreamReader reader = factory.createXMLStreamReader(byteArrayInputStream,"ISO8859-1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 3字节UTF-8序列xml转换无效的字节2 - Invalid byte 2 of 3-byte UTF-8 sequence xml transformation exception org.xml.sax.SAXParseException:3字节UTF-8序列的字节2无效 - org.xml.sax.SAXParseException: Invalid byte 2 of 3-byte UTF-8 sequence 当我执行Build项目时,三字节UTF-8序列的无效字节2 - Invalid byte 2 of a 3-byte UTF-8 sequence when i execute the Build project 解析 xml 文件时出现异常(3 字节 UTF-8 序列的字节 2 无效) - Exception when parsing xml file (Invalid byte 2 of 3-byte UTF-8 sequence) 怎么解决? com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:3字节UTF-8序列的无效字节3 - How to resolve ? com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence 2 字节 UTF-8 序列的无效字节 2 - invalid byte 2 of 2-byte UTF-8 sequence 检查UTF-8数据类型3字节或4字节Unicode - Checking UTF-8 data type 3-byte, or 4-byte Unicode MalformedByteSequenceException:1字节UTF-8序列的无效字节1 - MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence 消息:hadoop中1字节UTF-8序列的无效字节1 - Message: Invalid byte 1 of 1-byte UTF-8 sequence in hadoop 如何修复 1 字节 UTF-8 序列的无效字节 1 - How to fix Invalid byte 1 of 1-byte UTF-8 sequence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM