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

MrAndre :

I am trying to parse a xml using stax but the error I get is:

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?

Joni :

Your XML file is not encoded in 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:

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=389136&siteId=1