在Axis2中使用非合并解析器

时间:2012-12-20 05:01:04

标签: java axis2 axiom

有人知道如何在分析SOAP消息时让Axis2使用非合并的XMLStreamReader吗?

我正在编写读取大型base64二进制文本元素的代码。合并是默认行为,这会导致默认的XMLStreamReader将整个文本加载到内存中,而不是返回多个CHARACTERS事件。这样做的结果是,在运行以下代码时,我的堆空间不足:

reader = element.getTextAsStream( true );

com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next

中发生OutOfMemory错误
java.lang.OutOfMemoryError: Java heap space
    at com.sun.org.apache.xerces.internal.util.XMLStringBuffer.append(XMLStringBuffer.java:208)
    at com.sun.org.apache.xerces.internal.util.XMLStringBuffer.append(XMLStringBuffer.java:226)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanContent(XMLDocumentFragmentScannerImpl.java:1552)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2864)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:558)
    at org.apache.axiom.util.stax.wrapper.XMLStreamReaderWrapper.next(XMLStreamReaderWrapper.java:225)
    at org.apache.axiom.util.stax.dialect.DisallowDoctypeDeclStreamReaderWrapper.next(DisallowDoctypeDeclStreamReaderWrapper.java:34)
    at org.apache.axiom.util.stax.wrapper.XMLStreamReaderWrapper.next(XMLStreamReaderWrapper.java:225)
    at org.apache.axiom.util.stax.dialect.SJSXPStreamReaderWrapper.next(SJSXPStreamReaderWrapper.java:138)
    at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:668)
    at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:214)
    at org.apache.axiom.om.impl.llom.SwitchingWrapper.updateNextNode(SwitchingWrapper.java:1098)
    at org.apache.axiom.om.impl.llom.SwitchingWrapper.<init>(SwitchingWrapper.java:198)
    at org.apache.axiom.om.impl.llom.OMStAXWrapper.<init>(OMStAXWrapper.java:73)
    at org.apache.axiom.om.impl.llom.OMContainerHelper.getXMLStreamReader(OMContainerHelper.java:67)
    at org.apache.axiom.om.impl.llom.OMContainerHelper.getXMLStreamReader(OMContainerHelper.java:40)
    at org.apache.axiom.om.impl.llom.OMElementImpl.getXMLStreamReader(OMElementImpl.java:790)
    at org.apache.axiom.om.impl.llom.OMElementImplUtil.getTextAsStream(OMElementImplUtil.java:114)
    at org.apache.axiom.om.impl.llom.OMElementImpl.getTextAsStream(OMElementImpl.java:826)
    at org.example.UploadFileParser.invokeBusinessLogic(UploadFileParser.java:160)

2 个答案:

答案 0 :(得分:1)

尝试http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/tutorial/doc/SJSXP5.html,它也可以从Maven的中央存储库中获取

    <groupId>com.sun.xml.stream</groupId>
    <artifactId>sjsxp</artifactId>
    <version>1.0.2</version>

如果将它放在类路径上,它将覆盖默认的impl

更新还有另一种方法。创建自己的XMLInputFactory

public class XMLInputFactory extends
        com.sun.xml.internal.stream.XMLInputFactoryImpl {

    @Override
    public XMLStreamReader createXMLStreamReader(Reader reader)
            throws XMLStreamException {
        return super.createXMLStreamReader(reader);
    }
}

创建文件

META-INF/services/javax.xml.stream.XMLInputFactory

请注意,javax.xml.stream.XMLInputFactory是文件名。将您的类完全提升为该文件test.XMLInputFactory中的名称。请参阅http://docs.oracle.com/javase/1.4.2/docs/guide/jar/jar.html(服务提供商)

的详细信息

它将使Axis2加载您的XMLInputFactory。现在,您可以拦截任何方法调用并更改解析行为。

答案 1 :(得分:1)

这很容易做到。请参阅Axiom用户指南的以下部分:

http://ws.apache.org/axiom/userguide/ch04.html#factory.properties