我有以下代码:
public Object unmarshal(Source source, MimeContainer mimeContainer) throws XmlMappingException {
AttachmentUnmarshaller au = null;
if (this.mtomEnabled && mimeContainer != null) {
au = new MISMarshaller.MISAttachmentUnmarshaller(mimeContainer);
}
if (source instanceof SAXSource && ((SAXSource)source).getXMLReader() != null) {
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
return this.context.unmarshal(au, factory.createXMLStreamReader(source));
} catch (Exception var5) {
throw new UnmarshallingFailureException(var5.getMessage(), var5);
}
} else {
throw new IllegalStateException("Only StAX is supported for MIS marshaller. Use AXIOM message factory.");
}
}
在此行,我得到一个例外:
return this.context.unmarshal(au, factory.createXMLStreamReader(source));
这里是例外:
javax.xml.stream.XMLStreamException:无法为创建Stax阅读器传递的源-阅读器,输入流或系统ID都不是无障碍;不能使用其他类型的源(例如嵌入式SAX流)
在运行时,source
是StaxSource
的实例。
是否有解决此问题的方法?
从Spring 3.0开始,您可以使用StaxUtils(org.springframework.util.xml.StaxUtils)
类,而直接使用不推荐使用的StaxResult
类。我找到了一些示例,您可以对marshal
方法执行以下操作:
public void marshal(Object graph, Result result, MimeContainer mimeContainer) throws XmlMappingException {
try {
AttachmentMarshaller am = null;
if (this.mtomEnabled && mimeContainer != null) {
am = new MISMarshaller.MISAttachmentMarshaller(mimeContainer);
}
if (StaxUtils.isStaxResult(result) && StaxUtils.getXMLStreamWriter(result) != null) {
this.context.marshal(graph, am, StaxUtils.getXMLStreamWriter(result));
} else {
if (!(result instanceof SAXResult) || ((SAXResult)result).getHandler() == null) {
throw new IllegalStateException("Only StAX or SAX is supported for MIS marshaller. Use AXIOM message factory.");
}
this.context.marshal(graph, am, (SAXResult)result);
}
} catch (Exception var5) {
throw new MarshallingFailureException(var5.getMessage(), var5);
}
}
与以前的旧方法相反:
public void marshal(Object graph, Result result, MimeContainer mimeContainer) throws XmlMappingException {
try {
AttachmentMarshaller am = null;
if (this.mtomEnabled && mimeContainer != null) {
am = new MISMarshaller.MISAttachmentMarshaller(mimeContainer);
}
if (result instanceof StaxResult && ((StaxResult)result).getXMLStreamWriter() != null) {
this.context.marshal(graph, am, ((StaxResult)result).getXMLStreamWriter());
} else {
if (!(result instanceof SAXResult) || ((SAXResult)result).getHandler() == null) {
throw new IllegalStateException("Only StAX or SAX is supported for MIS marshaller. Use AXIOM message factory.");
}
this.context.marshal(graph, am, (SAXResult)result);
}
} catch (Exception var5) {
throw new MarshallingFailureException(var5.getMessage(), var5);
}
}