"Can not create Stax reader for the Source passed" 错误

"Can not create Stax reader for the Source passed" error

我有以下代码:

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: Can not create Stax reader for the Source passed -- neither reader, input stream nor system id was accessible; can not use other types of sources (like embedded SAX streams)

在 运行 时,sourceStaxSource 的实例。

有办法解决这个问题吗?

看起来从 Spring 3.0 开始,您可以直接使用 StaxUtils(org.springframework.util.xml.StaxUtils) class 来支持已弃用的 StaxResult class。我找到了一些例子,你可以为 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);
    }
}