Premature end of file using JAXB and Unmarshaller. Xml from the answer, it suits me

I do not know what to do next. Everything seems right; input Output.

I create an xml file and send some service for verification.

Answer:

11:10:34,922 INFO  [STDOUT] printing out the input stream
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Response>
    <Method name="XML/Release/New" time="2013-04-23T15:10:35.1446238Z">
        <ResponseStatus>100</ResponseStatus>
    </Method>
</Response>
finished printing out the input stream
11:10:34,922 INFO  [STDOUT] got the unmarshaller
11:10:34,925 ERROR [PRNDataAccessUtil] Caught an error: javax.xml.bind.UnmarshalException
 - with linked exception: [org.xml.sax.SAXParseException: Premature end of file.] : null

The code:

try {
            out = connection.getOutputStream();
            ByteArrayOutputStream bos = PRNPostNewsReleaseUtil.createNewsReleaseXml(newsRelease);
            bos.writeTo(out);

            JAXBContext context = JAXBContext.newInstance(Response.class.getPackage().getName());
            in = connection.getInputStream();

            BufferedReader inp = new BufferedReader(new InputStreamReader(in));
            System.out.println("printing out the input stream");
            String line;
            while((line = inp.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println("finished printing out the input stream");

            Unmarshaller unmarshaller = context.createUnmarshaller();
            response = (Response) unmarshaller.unmarshal(in);

        } catch (Exception ex) {
            log.error("Caught an error: " + ex + " : " + ex.getMessage());
            return null;
        } finally {
            if (null != in) connection.disconnect();
        }
+5
source share
3 answers

You get an error because it InputStreamwas expanded to the end during output. Assuming the buffer in yours BufferedReaderis large enough to contain the entire XML document, you can reset after the output and then cancel it.

+7
source

, JAXBContext, , , XML , unmarshaller.

, , JAXBContext , .

+3

One more thing worth noting here, even if you do not read the buffer explicitly in the code, but watch the expression that reads the input, it will have the same effect as increasing the stream header. I realized that after spending hours debugging this exception.

+1
source

All Articles