DocumentBuilder parse produces invalid byte 2 of 4

2019-07-14 04:04发布

问题:

I am trying to parse a bytearray which contains a string Impresión in XML

final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = builderFactory.newDocumentBuilder();
final Document document;
try (final InputStream stream = new ByteArrayInputStream(bytearray)) {
     document = builder.parse(stream); // gives Invalid byte 2 of 4-byte UTF-8 sequence error
}

It produces Invalid byte 2 of 4-byte UTF-8 sequence error. But when i have Unicode Impresi\u00f3n, it works fine. But i want to parse actual character.

Anybody help me to solve it.