Java JaxB Unmarshaller giving org.xml.sax.SAXParseException


private Course unmarshalCourse(InputStream is) throws CourseServiceException, IOException{
Course c=null;
try{
JAXBContext jc = JAXBContext.newInstance( "com.sakai.domain" );
Unmarshaller u = jc.createUnmarshaller();
String theString = IOUtils.toString(is, "UTF-8"); 
log.debug("---------xml"+theString);
Object value = u.unmarshal(is);
c=(Course)value;
log.debug("---------course"+c);
}catch(JAXBException e){
//je.printStackTrace();
throw new CourseServiceException(e, Error.CONFIG);
}
return c;
}

我正在将输入流作为 xml。当我尝试取消编组时,会触发以下错误。请帮助。

[org.xml.sax.SAXParseException; 行号: 1; 列号: 1; 文件过早结束。com.course.logic.CourseServiceException: javax.xml.bind.UnmarshalException - 带有链接的异常:[org.xml.sax.SAXParseException; 行号: 1; 列号: 1;文件过早结束。

该问题是由您使用InputStream is的方式引起的。

String theString = IOUtils.toString(is, "UTF-8"); 
log.debug("---------xml"+theString);
Object value = u.unmarshal(is);

首先,你通过读取它来消耗所有的输入流IOUtils.toString(is, "UTF-8").当然,在那之后你就在 流的结束。然后您尝试继续从此流中读取 由u.unmarshal(is).毫不奇怪,你会得到一个例外Premature end of file现在。

要解决此问题,请不要从InputStream is取消封送。 但从String theString中解组:

String theString = IOUtils.toString(is, "UTF-8"); 
log.debug("---------xml"+theString);
Object value = u.unmarshal(new StringReader(theString));

相关内容

最新更新