“Content is not allowed in prolog” when parsing perfectly valid XML on GAE

后端 未结 13 2089
抹茶落季
抹茶落季 2020-11-27 15:20

I\'ve been beating my head against this absolutely infuriating bug for the last 48 hours, so I thought I\'d finally throw in the towel and try asking here before I throw my

13条回答
  •  孤城傲影
    2020-11-27 16:17

    In my xml file, the header looked like this:

    
    

    In a test file, I was reading the file bytes and decoding the data as UTF-8 (not realizing the header in this file was utf-16) to create a string.

    byte[] data = Files.readAllBytes(Paths.get(path));
    String dataString = new String(data, "UTF-8");
    

    When I tried to deserialize this string into an object, I was seeing the same error:

    javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
    Message: Content is not allowed in prolog.
    

    When I updated the second line to

    String dataString = new String(data, "UTF-16");
    

    I was able to deserialize the object just fine. So as Romain had noted above, the encodings need to match.

提交回复
热议问题