[英]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.