开发者

Reading xml -file in UTF-8 format in Windows with Java gives "IOException: Invalid byte 2 of 2-byte UTF-8 sequence." -error

I have problem with my Java program. How I read xml -file which has "UTF-8" encoding. Program works correctly in Kubuntu but I doesn't work in Windows. Both OSes is writing xml -file correctly but parsing gives exception error in Windows.

String XMLFile = "ÄÄKKÖSET.xml"
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File (XMLFile));

Here is xml -file I need to parse:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<deck created="04/04/2011">
  <title>ääkköset</title>
  <code>ÄÄKKÖSET</code>
  <description>ääkköset</description>
  <author>ääkköset</author>
  <cards nextCardID="1">
    <card color="#1364F9" id="0">
      <question>ÄÄKKÖSET</question>
      <answer>ÄÄKKÖSET</answer>
    </card>
  </cards>
</deck>

How do I get to read xml -file with Java in Windows without getting I get "IOException: Invalid byte 2 of 2-byte U开发者_如何学JAVATF-8 sequence." -error?

Thanks in advance!


Invalid byte 2 of 2-byte UTF-8 sequence.

Your XML document has not been saved as UTF-8, the parser detects this (because not all byte sequences are legal UTF-8) and throws an error.

The solution is to save the file as UTF-8. It is not enough to declare the document as UTF-8 - the bytes the data is encoded to must match this declaration. By default, many text editors on Windows will default to saving data as ANSI.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜