eclipse org.xmlpull.v1.XmlPullParserException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10683363/
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-09-19 18:13:38  来源:igfitidea点击:

org.xmlpull.v1.XmlPullParserException

javaeclipseunmarshallingjibxxmlpullparser

提问by hari

I'm trying to bind an xml file(as a byte[]) to a java object. This is my code-

我正在尝试将一个 xml 文件(作为 a byte[])绑定到一个 java 对象。这是我的代码-

public voidinputConfigXML(String xmlfile, byte[] xmlData) {
    IBindingFactory bFact = BindingDirectory.getFactory(GroupsDTO.class);
                IUnmarshallingContext uctx = bFact.createUnmarshallingContext();
                groups = (GroupsDTO) uctx.unmarshalDocument(new ByteArrayInputStream(xmlData), "UTF8");
}

The unmarshalDocument()is giving me this exception. What do i do?

unmarshalDocument()是给我这个例外。我该怎么办?

FYI: Running as JUnit test case

仅供参考:作为 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)
        [...]

This is my code that forms byte[]-

这是我的代码,形成 byte[]-

void test() {
String xmlfile = "output.xml"
File file = new File(xmlfile);
byte[] xmlData = new byte[(int) file.length()];
groups = dra.inputConfigXML(xmlfile, xmlData);
}

回答by Christian Kuetbach

The ByteArrayInputstream is empty:

ByteArrayInputstream 为空:

only whitespace content allowed before start tag and not \u0 
(position: START_DOCUMENT seen \u0... @1:1) 

means, that a \u0 Bit was found as first char within the XML.

意味着,在 XML 中找到了一个 \u0 位作为第一个字符。

Ensure you have content within your byte[]and the UTF-8 don't start with a BOM.

确保您的内容中有内容byte[]并且 UTF-8 不以BOM开头。

I don't think, that the BOM is your problem here, but I often encountert regarding BOM and java.

我不认为 BOM 是你的问题,但我经常遇到关于 BOM 和 java 的问题。

update

更新

You don't fill the byte[]. You have to read the file-content into the byte[]: read this: File to byte[] in Java

你不填byte[]。您必须将文件内容byte[]读入: read this: File to byte[] in Java

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_VALUEyou will read a corrupt file.

顺便说一句:byte[] xmlData = new byte[(int) file.length()];是糟糕的代码风格,因为你会遇到更大的 XML 文件的问题。如果它们大于Integer.MAX_VALUE您将读取损坏的文件。

回答by Don Corley

Hari,
JiBX need characters as input. I think you have specified your encoding incorrectly. Try this code instead:

Hari、
JiBX 需要字符作为输入。我认为您错误地指定了您的编码。试试这个代码:

FileInputStream fis = new FileInputStream("output.xml");
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
groups = (GroupsDTO) uctx.unmarshalDocument(isr);

If you must use the code you have written, I would try outputting the text to the console (System.put.println(xxx)) to make sure you are decoding the utf-8 correctly.

如果您必须使用您编写的代码,我会尝试将文本输出到控制台 (System.put.println(xxx)) 以确保您正确解码 utf-8。

Don

大学教师

回答by Don Corley

Go to to mvn repository path and delete that folder for xml file.

转到 mvn 存储库路径并删除该文件夹以存放 xml 文件。