34

我正在尝试使用 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 中的一些特殊字符。如何解决这个问题?

4

14 回答 14

22

如何解决这个问题?

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

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

于 2013-03-21T11:11:35.277 回答
21
  1. 在记事本中打开xml
  2. 确保在文档的开头和结尾没有多余的空间。
  3. 选择文件 -> 另存为
  4. 选择另存为类型 -> 所有文件
  5. 输入文件名作为 abcd.xml
  6. 选择编码 - UTF-8 -> 点击保存
于 2014-11-21T08:43:18.707 回答
7

尝试:

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,只需将编码部分更改为好的部分。

于 2013-03-21T11:09:49.443 回答
6

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

于 2017-07-25T17:07:41.933 回答
2

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

//Some �  special characters found

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

于 2018-11-24T12:58:37.413 回答
1

我遇到了这个问题,但是文件是 UTF-8 格式的,只是以某种方式进入的字符没有以 UTF-8 编码。为了解决这个问题,我做了这个线程中所说的,即我验证了文件: 如何检查文件是否是有效的 UTF-8?

基本上你运行命令:

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

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

于 2015-12-03T13:29:13.130 回答
1
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

  
  
于 2020-07-28T12:15:50.030 回答
1

我有一个类似的问题。我将一些 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)));

请让我知道这对你有没有用。

于 2020-11-03T11:51:06.537 回答
0

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

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

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

于 2016-02-03T14:48:57.070 回答
0

我遇到了同样的问题,在对我的 XML 文件进行长时间调查后,我发现了问题:几乎没有像« ».

于 2016-02-17T23:47:42.880 回答
0

像我这样了解字符编码原理的人还阅读了 Joel 的文章,这篇文章很有趣,因为它包含错误的字符并且仍然无法弄清楚到底是什么(剧透警报,我是 Mac 用户),那么您的解决方案可以很简单删除您的本地存储库并再次克隆它

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

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

于 2017-11-15T18:30:50.627 回答
0

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

于 2019-02-13T09:40:03.200 回答
0

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

   implementation 'org.apache.maven.plugins:maven-surefire-plugin:2.4.3'
于 2020-03-02T20:27:21.530 回答
0

这个错误让我在生产中感到惊讶......

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

这是一种方法:

...    
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>
...
于 2021-03-10T23:54:29.577 回答