简体   繁体   English

在Windows中使用Java读取UTF-8格式的xml -file会给出“ IOException:2字节UTF-8序列的无效字节2。” -error

[英]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.

相关问题 2 字节 UTF-8 序列的无效字节 2 - invalid byte 2 of 2-byte UTF-8 sequence 2 字节 UTF-8 Java 的无效字节 2,序列错误取决于 Windows/IntelliJ - Invalid byte 2 of 2-byte UTF-8 Java, sequence error depending on Windows/IntelliJ MalformedByteSequenceException:2字节UTF-8序列的无效字节2 - MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence 2字节UTF-8序列的无效字节2:如何查找字符 - Invalid byte 2 of 2-byte UTF-8 sequence : How to find the character Android studio 2字节UTF-8序列的无效字节2 - Android studio Invalid byte 2 of 2-byte UTF-8 sequence 从URL解析RSS给我“ 2字节UTF-8序列的无效字节2” - Parse RSS from URLs gives me “Invalid byte 2 of 2-byte UTF-8 sequence” 嵌套的异常是org.xml.sax.SAXParseException:2字节UTF-8序列的无效字节2 - nested exception is org.xml.sax.SAXParseException: Invalid byte 2 of 2-byte UTF-8 sequence 2字节UTF-8序列的无效字节2:XML保存为字符串变量 - Invalid byte 2 of 2-byte UTF-8 sequence: XML saved as String varible JAXB和UTF-8解组异常“ 2字节UTF-8序列的无效字节2” - JAXB & UTF-8 Unmarshal exception “Invalid byte 2 of 2-byte UTF-8 sequence” 验证XML文档会导致“1字节UTF-8序列的无效字节1”。 - Validating XML doc results in “Invalid byte 1 of 1-byte UTF-8 sequence.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM