Get an InputStream/io.Reader from OMElement object

空扰寡人 提交于 2019-12-23 10:06:36

问题


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!

Thanks

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();

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


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/8221892/get-an-inputstream-io-reader-from-omelement-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!