org.xmlpull.v1.XmlPullParserException [英] org.xmlpull.v1.XmlPullParserException
问题描述
我正在尝试将xml文件(作为 byte []
)绑定到java对象。这是我的代码 -
I'm trying to bind an xml file(as a byte[]
) to a java object. This is my code-
public voidinputConfigXML(String xmlfile, byte[] xmlData) {
IBindingFactory bFact = BindingDirectory.getFactory(GroupsDTO.class);
IUnmarshallingContext uctx = bFact.createUnmarshallingContext();
groups = (GroupsDTO) uctx.unmarshalDocument(new ByteArrayInputStream(xmlData), "UTF8");
}
unmarshalDocument()
正在给我这个例外。我该怎么办?
The unmarshalDocument()
is giving me this exception. What do i do?
FYI:作为JUnit测试用例运行
以下是堆栈跟踪 -
The following is the stacktrace -
Error parsing document (line 1, col 1)
org.xmlpull.v1.XmlPullParserException: only whitespace content allowed before start tag and not \u0 (position: START_DOCUMENT seen \u0... @1:1)
at org.xmlpull.mxp1.MXParser.parseProlog(MXParser.java:1519)
at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1395)
at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)
at org.jibx.runtime.impl.XMLPullReaderFactory$XMLPullReader.next(XMLPullReaderFactory.java:291)
at org.jibx.runtime.impl.UnmarshallingContext.toStart(UnmarshallingContext.java:451)
at org.jibx.runtime.impl.UnmarshallingContext.unmarshalElement(UnmarshallingContext.java:2755)
at org.jibx.runtime.impl.UnmarshallingContext.unmarshalDocument(UnmarshallingContext.java:2905)
at abc.dra.DRAAPI.inputConfigXML(DRAAPI.java:31)
at abc.dra.XMLToObject_Test.test(XMLToObject_Test.java:34)
[...]
这是我的代码,形成byte [] -
This is my code that forms byte[]-
void test() {
String xmlfile = "output.xml"
File file = new File(xmlfile);
byte[] xmlData = new byte[(int) file.length()];
groups = dra.inputConfigXML(xmlfile, xmlData);
}
推荐答案
ByteArrayInputstream为空:
The ByteArrayInputstream is empty:
only whitespace content allowed before start tag and not \u0
(position: START_DOCUMENT seen \u0... @1:1)
表示找到了一个\ u0位作为XML中的第一个字符。
means, that a \u0 Bit was found as first char within the XML.
确保您的 byte []
内容并且UTF-8不以< a href =http://en.wikipedia.org/wiki/Byte_Order_Mark =nofollow noreferrer> BOM 。
Ensure you have content within your byte[]
and the UTF-8 don't start with a BOM.
我不认为BOM是你的问题,但我经常遇到有关BOM和java的问题。
I don't think, that the BOM is your problem here, but I often encountert regarding BOM and java.
更新
您没有填写 byte []
。你必须将文件内容读入 byte []
:
读取: Java中的文件到byte []
You don't fill the byte[]
. You have to read the file-content into the byte[]
:
read this: File to byte[] in Java
顺便说一下: byte [ ] xmlData = new byte [(int)file.length()];
是错误的代码风格,因为你会遇到更大的XML文件的问题。如果它们大于 Integer.MAX_VALUE
,您将读取损坏的文件。
By the way: byte[] xmlData = new byte[(int) file.length()];
is bad code-style, becaus you will run into problems with larger XML-files. If they are larger than Integer.MAX_VALUE
you will read a corrupt file.
这篇关于org.xmlpull.v1.XmlPullParserException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!