我遇到了com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException
的XML文件。我使用调试器逐步完成了Xerces代码,并缩小了这个区域的范围。我能够通过删除文档中的“智能引号”字符来确定该文档变得可解析。
该文件没有DTD。 Notepad ++将其固定为“ANSI as UTF-8”。 Firefox将其视为“西方”。我记得在大学里一个不那么令人惊叹的讲座中,UTF-8被设计为向后兼容单字节编码系统。我也看到on this chart,字节序列e2 80 9d
实际上代表了“正确的双引号”,但即使我看不到编码问题,我在想那里是一个。
我从Xerces获得的异常消息是Invalid byte 3 of 3-byte UTF-8 sequence.
它会从UTF8Reader第435行的invalidByte(3, 3, b2)
调用中抛出。当我试图完全理解这种方法的逻辑时,我的大脑开始融化我的耳朵,所以我可能会遗漏一些东西,但正如我在上面提到的字节3(0x90)。至少上面的顺序,根据UTF-8表有效。
以下是十六进制编辑器中显示双引号的文件段:
我尝试了以下内容:
<?xml version="1.0" encoding="UTF-8"?>
表示为无效的字节似乎是63(0x3F?)
我还尝试将此智能引号字符添加到以前可解析的文档中。正如预期的那样,它会使解析器抛出相同的异常。
堆栈追踪:
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:687)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:435)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1753)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(XMLEntityScanner.java:1426)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2815)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
...
更新 我仍然需要找到一种方法来安全地将其转换为String。我使用Notepad ++将文件编码为UTF-8。下面的代码成功地将字节加载到String中(我在Eclipse中调试时可以看到在String中读取XML),但现在我得到了带有不同参数的MalformedByteSequenceException。这一次,我可以发布代码和我正在使用的XML:
File file = new File("ccd.xml");
byte[] ccdBytes = org.apache.commons.io.FileUtils.readFileToByteArray(file);
String ccdString = new String(ccdBytes, Charset.forName("UTF-8"));
CDAUtil.load(new ByteArrayInputStream(IOUtils.toByteArray(ccdString))); //method that's doing the parsing
堆栈追踪:
Exception in thread "main" com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:687)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:557)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1753)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(XMLEntityScanner.java:1426)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2815)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
at org.openhealthtools.mdht.emf.runtime.resource.impl.FleXMLLoadImpl.load(FleXMLLoadImpl.java:55)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:180)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1494)
at org.openhealthtools.mdht.uml.cda.util.CDAUtil.load(CDAUtil.java:268)
at org.openhealthtools.mdht.uml.cda.util.CDAUtil.load(CDAUtil.java:250)
at org.openhealthtools.mdht.uml.cda.util.CDAUtil.load(CDAUtil.java:238)
然而,
CDAUtil.load(new FileInputStream(new File("ccd.xml")));
作品
答案 0 :(得分:6)
您没有告诉我们您如何将文件传递给Xerces。你可以用不同的方式做不同的结果。您可以阅读有关xml编码问题here
的更详细说明我建议你做以下事情:
<?xml version="1.0"
encoding="UTF-8"?>
作为第一行这可以解决问题,如果您可以通过将文件传递给解析器的代码,则更容易找到问题。
这些步骤解决了这个问题,因为只有在使用InputStream(即字节流)时,解析器才会考虑带有编码声明的xml中的第一行。如果您读取字节流,则需要编码声明,以指定如何将字节转换为字符。
如果你传递一个String,那么第一行是无用的,因为你传递的是一串字符,而且不需要编码。
如果要使用String,则必须将该文件作为InputStream读取并转换为指定字符集的Reader(类似于InputStreamReader inputStreamReader= new InputStreamReader(xmlFileInputStream,"UTF-8");
)
我的猜测是你收到了错误,因为你没有指定charset而且Java选择了你的一个操作系统(Windows-1252)。
答案 1 :(得分:2)
当我的输入文件中出现实际的UTF-8编码错误时,我才能收到该错误消息。因此,我认为文件中存在一个您无法找到的实际错误。
这是我的测试代码:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class ParseAXml {
public static void main(String argv[]) throws Exception {
String xmlFile = argv[0];
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlFile);
System.out.println("Parsed Successfully");
}
}
当我传递一个正确的文件时 - 包含智能引号 - 我得到了预期的Parsed Successfully
消息。这是我的常规测试文件:
$ hexdump -C tmp.xml
00000000 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 |<?xml version="1|
00000010 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 55 54 |.0" encoding="UT|
00000020 46 2d 38 22 3f 3e 0a 3c 74 68 69 6e 67 3e 3c 61 |F-8"?>.<thing><a|
00000030 3e 54 68 69 73 20 e2 80 9c 71 75 6f 74 65 e2 80 |>This ...quote..|
00000040 9d 20 63 6f 75 6c 64 20 67 65 74 20 74 72 69 63 |. could get tric|
00000050 6b 79 3c 2f 61 3e 3c 2f 74 68 69 6e 67 3e 0a |ky</a></thing>.|
0000005f
当我测试一个错误的文件时 - 通过在偏移量0x38处修改字节来实现 - 我得到你看到的异常:
$ java ParseAXml tmp.err.xml
Exception in thread "main" com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:687)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:435)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1753)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(XMLEntityScanner.java:1426)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2807)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
at ParseAXml.main(ParseAXml.java:10)
为了帮助你,我写了一个简短的java程序,试图找到格式错误的字节:
import java.nio.*;
import java.nio.charset.*;
import java.io.*;
public class FindBadUTF8 {
public static void main(String argv[]) throws Exception {
String filename = argv[0];
InputStream inStream = new FileInputStream(filename);
CharsetDecoder d=Charset.forName("UTF-8").newDecoder();
CharBuffer out = CharBuffer.allocate(1);
ByteBuffer in = ByteBuffer.allocate(10);
in.clear();
long offset = 0L;
while (true) {
int read = inStream.read();
if (read != -1) {
in.put((byte)read);
}
out.clear();
in.flip();
CoderResult cr = d.decode(in, out, (read == -1));
if (cr.isError()) {
if (read != -1) {
System.out.println("Error at offset " + offset + ": " + cr);
return;
} else {
System.out.println("Error at end-of-file: " + cr);
return;
}
}
if (cr.isUnderflow()) {
in.position(in.limit());
in.limit(in.capacity());
} else {
in.clear();
}
if (read == -1) {
break;
}
offset += 1L;
}
System.out.println("OK");
}
}
该程序在针对我的示例文件运行时出现错误,这给了我:
$ java FindBadUTF8 tmp.err.xml
Error at offset 56: MALFORMED[2]
实际上,偏移56(十六进制为0x38)是我修改的字节:
$ hexdump -C tmp.err.xml
00000000 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 |<?xml version="1|
00000010 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 55 54 |.0" encoding="UT|
00000020 46 2d 38 22 3f 3e 0a 3c 74 68 69 6e 67 3e 3c 61 |F-8"?>.<thing><a|
00000030 3e 54 68 69 73 20 e2 80 ff 71 75 6f 74 65 e2 80 |>This ...quote..|
00000040 9d 20 63 6f 75 6c 64 20 67 65 74 20 74 72 69 63 |. could get tric|
00000050 6b 79 3c 2f 61 3e 3c 2f 74 68 69 6e 67 3e 0a |ky</a></thing>.|
0000005f