从OMElement对象获取InputStream/io.Reader [英] Get an InputStream/io.Reader from OMElement object

查看:241
本文介绍了从OMElement对象获取InputStream/io.Reader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OMElement对象,从中我想得到一个InputStream或阅读器对象.我想要的是从我拥有的OMElement中流式传输xml,而不会加载到内存中.我只能从中获取XMLStreamReader对象.但是我找不到从中删除InputStream/Reader的方法.

I have a OMElement object and from that I want to get an InputStream or reader object. What I want is to stream the xml from the OMElement which I have, without getting loaded into memory. I only could be able to get XMLStreamReader object from that. But I can't find a way to get InputStream/Reader out of that.

OMElement element /*I have this object */
XMLStreamReader xmlreader = element.getXMLStreamReaderWithoutCaching();

有人可以帮助我吗?我真的很感激!

Can anyone please help me ? I really appreciate!

谢谢

PS:我已经找到了一种获取InputStream的方法,但是它提供了一个异常,需要我帮忙解决它.

PS: I have come a way to get an InputStream but it gives an exception which I need help to resolve it.

XMLStreamReader xmlReader = element.getXMLStreamReader(false);

  try {
      if (xmlReader.getEventType() == XMLStreamReader.START_DOCUMENT) {
                xmlReader.next();
       }
      DataHandler handler = XMLStreamReaderUtils.getDataHandlerFromElement(xmlReader);
      handler.getInputStream();

对getDataHandlerFromElement的调用生成异常 javax.xml.stream.XMLStreamException:base64解码期间发生错误

The call to getDataHandlerFromElement generate the exception javax.xml.stream.XMLStreamException: Error during base64 decoding

推荐答案

getDataHandlerFromElement方法的目的是解码元素中包含的base64编码二进制数据,这不是您想要的.

The purpose of the getDataHandlerFromElement method is to decode base64 encoded binary data contained in an element, which is not what you want.

Axiom当前没有流式拉取序列化器(并且我不知道其他任何支持该序列化的XML库).它仅支持以推送模式序列化为XML,即通过写入OutputStream或Writer.您可以在单独的线程中进行序列化,并使用PipedOutputStream/PipedInputStream(类似于激活对未由数据源支持的DataHandler的DataHandler#getInputStream()进行的操作).这样可以满足恒定的内存需求,但是我想在您的情况下,使用单独的线程导致的开销是不可接受的.

Axiom currently doesn't have a streaming pull serializer (and I don't know any other XML library that supports this). It only supports serializing to XML in push mode, i.e. by writing to an OutputStream or Writer. You could do the serialization in a separate thread and use a PipedOutputStream/PipedInputStream (similar to what Activation does for DataHandler#getInputStream() for a DataHandler that is not backed by a DataSource). This would satisfy the constant memory requirement, but I guess that the overhead caused by using a separate thread would not be acceptable in your case.

我曾经考虑过实现这种拉序列化程序的可能性,并且我认为这样做(在不使用单独线程的情况下)在技术上是可行的.请打开对Axiom的功能请求,以便我们可以在下一个版本中实现该功能.我认为这将是对Axiom的有趣补充.

I thought about the possibility to implement such a pull serializer in the past, and I think it's technically feasible to do that (without using a separate thread). Please open a feature request for Axiom so that we can implement that in one of the next releases. I think it would be an interesting addition to Axiom.

这篇关于从OMElement对象获取InputStream/io.Reader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆