Java 遇到无效字符“�”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/968222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:43:12  来源:igfitidea点击:

Invalid character '&#x0' encountered

javaxmlblackberryjava-me

提问by iOSDev

I am getting following exception while parsing the xml.

解析xml时出现以下异常。

Fatal error at line -1  Invalid character '&#x0' encountered. No stack trace

I have Xml data in string format and I am parsing it using DOM parser. I am parsing data which is a response from Java server to a Blackberry client. I also tried parsing with SAX parser,but problem is not resolved. Please help.

我有字符串格式的 Xml 数据,我正在使用 DOM 解析器解析它。我正在解析数据,这是从 Java 服务器到 Blackberry 客户端的响应。我也试过用 SAX 解析器解析,但问题没有解决。请帮忙。

采纳答案by iOSDev

I got the solution,

我得到了解决方案,

I just trimmed it with trim() and it worked perfectly fine with me.

我只是用 trim() 修剪它,它对我来说效果很好。

回答by iOSDev

InputStream xml = new ByteArrayInputStream(xmlData.getBytes());
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(xml);
doc.getDocumentElement().normalize();
xml.close();

Above is the code I am using for parsing.

以上是我用于解析的代码。

回答by Jon Skeet

Your code currently calls getBytes()using the platform default encoding - that's very rarely a good idea. Find out what the encoding of the data really is, and use that. (It's likely to be UTF-8.)

您的代码当前getBytes()使用平台默认编码进行调用- 这很少是一个好主意。找出数据的真正编码是什么,然后使用它。(很可能是 UTF-8。)

If the Blackberry includes DocumentBuilder.parse(InputSource), that would be preferable:

如果黑莓包括DocumentBuilder.parse(InputSource),那将是可取的:

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
StringReader reader = new StringReader(xmlData);
try {
    Document doc = docBuilder.parse(xml); 
    doc.getDocumentElement().normalize();
} finally {
    reader.close();
}

If that doesn't work, have a veryclose look at your string, e.g. like this:

如果不工作,有一个非常在你的字符串,例如,像这样近距离观察:

for (int i=0; i < xmlData.length(); i++) {
    // Use whatever logging you have on the Blackberry
    System.out.println((int) xmlData.charAt(i));
}

It's possible that the problem is reading the response from the server - if you're reading it badly, you could have Unicode nulls (\u0000) in your string, which may not appear obviously in log/debug output, but would cause the error you've shown.

问题可能出在从服务器读取响应 - 如果您读取的响应不好,您的字符串中可能有 Unicode 空值 (\u0000),这可能不会在日志/调试输出中明显出现,但会导致错误你已经展示过了。

EDIT: I've just seen that you're getting the base64 data in the first place - so why convert it to a string and then back to bytes? Just decode the base64 to a byte array and then use that as the basis of your ByteArrayInputStream. Then you never have to deal with a text encoding in the first place.

编辑:我刚刚看到您首先获得了 base64 数据-那么为什么要将其转换为字符串然后再转换回字节呢?只需将 base64 解码为字节数组,然后将其用作ByteArrayInputStream. 那么你永远不必首先处理文本编码。

回答by Thorbj?rn Ravn Andersen

You have a null character in your character stream, i.e. char(0) which is not valid in an XML-document. If this is not present in the original string, then it is most likely a character decoding issue.

您的字符流中有一个空字符,即在 XML 文档中无效的 char(0)。如果这在原始字符串中不存在,则很可能是字符解码问题。