gpt4 book ai didi

Java JaxB Unmarshaller 给出 org.xml.sax.SAXParseException 文件过早结束

转载 作者:行者123 更新时间:2023-12-01 19:53:25 27 4
gpt4 key购买 nike

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; lineNumber: 1; columnNumber: 1; Premature end of file.] com.course.logic.CourseServiceException: javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.]

最佳答案

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

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

首先,您通过读取来消耗所有的InputStreamIOUtils.toString(is, "UTF-8")。当然,在那之后你就在流结束。然后你尝试继续从这个流中读取通过u.unmarshal(is)。毫不奇怪,你会得到一个异常(exception)现在文件过早结束

要解决此问题,请不要从 InputStream is 中解码。但从 String theString 解码:

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

关于Java JaxB Unmarshaller 给出 org.xml.sax.SAXParseException 文件过早结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50489853/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com