【问题标题】:How to fix Invalid byte 1 of 1-byte UTF-8 sequence如何修复 1 字节 UTF-8 序列的无效字节 1
【发布时间】:2013-03-10 20:33:00
【问题描述】:

我正在尝试使用 java 方法从 db 中获取以下 xml,但出现错误

用于解析 xml 的代码

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

InputSource is = new InputSource(new ByteArrayInputStream(cond.getBytes()));

Document doc = db.parse(is);

Element elem = doc.getDocumentElement();

// here we expect a series of <data><name>N</name><value>V</value></data>
NodeList nodes = elem.getElementsByTagName("data");

TableID jobId = new TableID(_processInstanceId);
Job myJob = Job.queryByID(_clientContext, jobId, true);

if (nodes.getLength() == 0) {
    log(Level.DEBUG, "No data found on condition XML");

}

for (int i = 0; i < nodes.getLength(); i++) {
    // loop through the <data> in the XML

    Element dataTags = (Element) nodes.item(i);
    String name = getChildTagValue(dataTags, "name");
    String value = getChildTagValue(dataTags, "value");

    log(Level.INFO, "UserData/Value=" + name + "/" + value);

    myJob.setBulkUserData(name, value);
}

myJob.save();

数据

<ContactDetails>307896043</ContactDetails>
<ContactName>307896043</ContactName>
<Preferred_Completion_Date>
</Preferred_Completion_Date>
<service_address>A-End Address: 1ST HELIERST HELIERJT2 3XP832THE CABLES 1 POONHA LANEST HELIER JE JT2 3XP</service_address>
<ServiceOrderId>315473043</ServiceOrderId>
<ServiceOrderTypeId>50</ServiceOrderTypeId>
<CustDesiredDate>2013-03-20T18:12:04</CustDesiredDate>
<OrderId>307896043</OrderId>
<CreateWho>csmuser</CreateWho>
<AccountInternalId>20100333</AccountInternalId>
<ServiceInternalId>20766093</ServiceInternalId>
<ServiceInternalIdResets>0</ServiceInternalIdResets>
<Primary_Offer_Name  action='del'>MyMobile Blue &#163;44.99 [12 month term]</Primary_Offer_Name>
<Disc_Reason  action='del'>8</Disc_Reason>
<Sup_Offer  action='del'>80000257</Sup_Offer>
<Service_Type  action='del'>A-01-00</Service_Type>
<Priority  action='del'>4</Priority>
<Account_Number  action='del'>0</Account_Number>
<Offer  action='del'>80000257</Offer>
<msisdn  action='del'>447797142520</msisdn>
<imsi  action='del'>234503184</imsi>
<sim  action='del'>5535</sim>
<ocb9_ARM  action='del'>false</ocb9_ARM>
<port_in_required  action='del'>
</port_in_required>
<ocb9_mob  action='del'>none</ocb9_mob>
<ocb9_mob_BB  action='del'>
</ocb9_mob_BB>
<ocb9_LandLine  action='del'>
</ocb9_LandLine>
<ocb9_LandLine_BB  action='del'>
</ocb9_LandLine_BB>
<Contact_2>
</Contact_2>
<Acc_middle_name>
</Acc_middle_name>
<MarketCode>7</MarketCode>
<Acc_last_name>Port_OUT</Acc_last_name>
<Contact_1>
</Contact_1>
<Acc_first_name>.</Acc_first_name>
<EmaiId>
</EmaiId>

错误

 org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.

我在一些线程中读到这是因为 xml 中的一些特殊字符。 如何解决这个问题?

【问题讨论】:

  • 您可能已经注意到,如果没有正确的格式,您的问题很难理解。
  • 您没有显示任何代码并没有帮助,但我怀疑您的 XML 文件基本上是无效的。我怀疑它声称是 UTF-8,但 不是 UTF-8。您应该修复产生错误文件的任何内容。
  • 肯定检查数据库;如果正确存储为 UTF-8,请检查 java 连接器是否需要设置为 UTF-8(对于 MySQL 也是如此)。如果数据库定义错误,请努力切换到 UTF-8,因为它更通用。
  • 嗨,你能告诉我这将在哪里定义 db
  • 你能显示输入前几十个字节的十六进制转储吗?

标签: java xml xml-parsing


【解决方案1】:

如何解决这个问题?

使用正确的字符编码读取数据。该错误消息意味着您正试图以 UTF-8 格式读取数据(有意或因为这是未指定 &lt;?xml version="1.0" encoding="somethingelse"?&gt; 的 XML 文件的默认编码),但它实际上采用不同的编码,例如 ISO- 8859-1 或 Windows-1252。

为了能够建议您应该如何执行此操作,我必须查看您当前用于读取 XML 的代码。

【讨论】:

  • 我在尝试使用以下代码解析 xml 时遇到此错误
  • 谢谢大家,我设法解决了这个问题。通过在解析 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 之前将编码设置为 ISO-8859-1; DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(new ByteArrayInputStream(cond.getBytes())); is.setEncoding("ISO-8859-1"); 将这一行添加到现有代码 Document doc = db.parse(is);元素 elem = doc.getDocumentElement();
【解决方案2】:
  1. 在记事本中打开xml
  2. 确保文档的开头和结尾没有多余的空间。
  3. 选择文件 -> 另存为
  4. 选择保存类型 -> 所有文件
  5. 将文件名输入为 abcd.xml
  6. 选择编码 - UTF-8 -> 点击保存

【讨论】:

  • 值得一提的是,这在记事本上有效,在记事本++上无效
  • 解决了我的问题。
  • 非常简单。很有用
【解决方案3】:

试试:

InputStream inputStream= // Your InputStream from your database.
Reader reader = new InputStreamReader(inputStream,"UTF-8");

InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");

saxParser.parse(is, handler);

如果不是 UTF-8,只需将编码部分更改为好的。

【讨论】:

  • 我认为这是最好的答案,因为它允许为所有类型的 InputStreams 处理描述的错误,而不仅仅是文件。
【解决方案4】:

我将 xml 作为字符串获取并使用 xml.getBytes() 并收到此错误。更改为 xml.getBytes(Charset.forName("UTF-8")) 对我有用。

【讨论】:

  • 这对我有用。就我的问题而言,其他人都是“错误的”。我在做和你一样的事情。以字符串形式读取文件,获取非 UTF8 字节并获取 SAX 错误。 getBytes("UTF-8") 起作用了。
【解决方案5】:

我在我的 JSF 应用程序中遇到了同样的问题,即在 XMHTL 页面中有一个包含一些特殊字符的注释行。当我在 Eclipse 中比较以前的版本时,它有一条评论,

//Some �  special characters found

删除了这些字符,页面加载正常。主要与XML文件有关,请与工作版本进行比较。

【讨论】:

    【解决方案6】:

    我遇到了这个问题,但文件是 UTF-8 格式的,只是以某种方式输入了未以 UTF-8 编码的字符。为了解决这个问题,我做了这个线程中所说的,即我验证了文件: How to check whether a file is valid UTF-8?

    基本上你运行命令:

    $ iconv -f UTF-8 your_file -o /dev/null

    如果有些东西不是用 UTF-8 编码的,它会给你行号和行号,以便你找到它。

    【讨论】:

      【解决方案7】:
      This error comes when you are trying to load jasper report file with the extension .jasper
      For Example 
      c://reports//EmployeeReport.jasper"
      
      While you should load jasper report file with the extension .jrxml
      For Example 
      c://reports//EmployeeReport.jrxml"
      [See Problem Screenshot ][1] [1]: https://i.stack.imgur.com/D5SzR.png
      [See Solution Screenshot][2] [2]: https://i.stack.imgur.com/VeQb9.png
      
        
        
      

      【讨论】:

        【解决方案8】:

        我遇到了类似的问题。 我将一些 xml 保存在一个文件中,当将其读入 DOM 文档时,由于特殊字符而失败。然后我用下面的代码来修复它:

        String enco = new String(Files.readAllBytes(Paths.get(listPayloadPath+"/Payload.xml")), StandardCharsets.UTF_8);
        
        Document doc = builder.parse(new ByteArrayInputStream(enco.getBytes(StandardCharsets.UTF_8)));
        

        让我知道它是否适合你。

        【讨论】:

          【解决方案9】:

          由于 Ant 构建,我碰巧遇到了这个问题。

          Ant 构建获取文件并将filterchain expandproperties 应用于它。在此文件过滤期间,我的 Windows 机器的隐式默认非 UTF-8 字符编码用于生成过滤后的文件 - 因此无法正确映射其字符集之外的字符。

          一种解决方案是为 Ant 提供一个明确的 UTF-8 环境变量。 在 Cygwin 中,在启动 Ant 之前:export ANT_OPTS="-Dfile.encoding=UTF-8"

          【讨论】:

            【解决方案10】:

            我也遇到了同样的问题,在对我的 XML 文件进行长时间调查后,我发现了问题:像«» 这样的未转义字符很少。

            【讨论】:

              【解决方案11】:

              像我这样了解字符编码原理的人,also read Joel's article 这很有趣,因为它contains wrong characters anyway仍然无法弄清楚到底是什么(剧透警告,我是 Mac 用户)那么您的解决方案可以像删除本地存储库并再次克隆它一样简单。

              自上次运行正常以来,我的代码库没有改变,因此考虑到我们的构建系统从未抱怨过,出现 UTF 错误是没有意义的......直到我记得我不小心拔掉了我的电脑几天前 IntelliJ Idea 和整个运行(Java/Tomcat/Hibernate)

              我的 Mac 做得非常出色,我假装什么都没发生,我照常营业,但底层文件系统不知何故损坏了。浪费了一整天的时间试图弄清楚这一点。我希望它可以帮助某人。

              【讨论】:

                【解决方案12】:

                我有同样的问题。我的问题是它在 WebLogic 服务器的 statWeblogic.cmd 文件中的 JAVA_OPTION 下缺少“-Dfile.encoding=UTF8”参数。

                【讨论】:

                  【解决方案13】:

                  您有一个需要删除的库 像下面的库

                     implementation 'org.apache.maven.plugins:maven-surefire-plugin:2.4.3'
                  

                  【讨论】:

                    【解决方案14】:

                    这个错误在生产中让我吃惊...

                    错误是因为字符编码错误,所以最好的解决方案是实现一种自动检测输入字符集的方法。

                    这是一种方法:

                    ...    
                    import org.xml.sax.InputSource;
                    ...
                    
                    InputSource inputSource = new InputSource(inputStream);
                    someReader(
                        inputSource.getByteStream(), inputSource.getEncoding()
                      );
                    

                    输入样本:

                    <?xml version="1.0" encoding="utf-16"?>
                    <rss xmlns:dc="https://purl.org/dc/elements/1.1/" version="2.0">
                    <channel>
                    ...
                    

                    【讨论】:

                    • inputSource.getEncoding() 不“检测”编码。它只会返回提供的内容。
                    猜你喜欢
                    • 2018-04-10
                    • 2018-12-26
                    • 2013-07-23
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-12-10
                    • 2010-11-22
                    相关资源
                    最近更新 更多