【问题标题】:"Can not create Stax reader for the Source passed" error“无法为已通过的源创建 Stax 阅读器”错误
【发布时间】:2020-01-07 19:30:05
【问题描述】:

我有以下代码:

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 流)

在运行时,sourceStaxSource 的一个实例。

有没有办法解决这个问题?

【问题讨论】:

    标签: sax stax


    【解决方案1】:

    看起来从 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 2015-11-17
      • 2012-04-28
      • 1970-01-01
      • 2018-05-24
      • 2018-05-27
      相关资源
      最近更新 更多