org.xmlpull.v1.XmlPullParserException

2020-05-02 02:07发布

问题:

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");
}

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

FYI: Running as JUnit test case

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[]-

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

回答1:

The ByteArrayInputstream is empty:

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.

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

I don't think, that the BOM is your problem here, but I often encountert regarding BOM and 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

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.



回答2:

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

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.

Don



回答3:

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