[英]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. 我的Java程序有问题。 How I read xml -file which has "UTF-8" encoding.
我如何读取具有“ UTF-8”编码的xml -file。 Program works correctly in Kubuntu but I doesn't work in Windows.
该程序在Kubuntu中可以正常工作,但在Windows中却无法正常工作。 Both OSes is writing xml -file correctly but parsing gives exception error in Windows.
两个操作系统都正确地编写了xml文件,但是在Windows中解析会出现异常错误。
String XMLFile = "ÄÄKKÖSET.xml"
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File (XMLFile));
Here is xml -file I need to parse: 这是我需要解析的xml -file:
<?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 UTF-8 sequence." 我如何在Windows中使用Java读取xml -file而没有得到“ IOException:2字节UTF-8序列的无效字节2”。 -error?
-错误?
Thanks in advance! 提前致谢!
Invalid byte 2 of 2-byte UTF-8 sequence.
2字节UTF-8序列的无效字节2。
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. 您的XML文档尚未保存为UTF-8,解析器会检测到此错误(因为并非所有字节序列都是合法的UTF-8),并引发错误。
The solution is to save the file as UTF-8. 解决方案是将文件另存为UTF-8。 It is not enough to declare the document as UTF-8 - the bytes the data is encoded to must match this declaration.
仅将文档声明为UTF-8是不够的-数据编码后的字节必须与该声明匹配。 By default, many text editors on Windows will default to saving data as ANSI.
默认情况下,Windows上的许多文本编辑器默认将数据保存为ANSI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.