Unfortunately your question doesn't provide a clear description of the actual problem you are trying to solve. Instead it describes an issue with what you believe to be the solution to your problem. Therefore I can only try to reconstruct the problem based on the comments you made in response to Ian Roberts.
If my interpretation of these comments is correctly, then the problem is as follows. You have an XML document that contains an element with a long sequence of characters, which is structured into multiple lines:
<some_element>
line 1
line 2
line 3
...
line N
</some_element>
You want to process the content of the element line by line, but N is large, so that you need to find a memory efficient way to do that, i.e. an approach that avoids loading the entire content into memory.
The code snippet you have provided shows that you took a wrong direction when trying to solve that problem. The code serializes the OMElement
representing some_element
and then creates an InputStream
/Reader
from the serialized output. However, that would also contain the start and end tags for some_element
, which is not what you want. Instead you are only interested in the content of the element. If you look at the OMElement
interface, you can see that it actually defines a method that returns that content as a Reader
. It is called getTextAsStream
and the Javadoc explains how to use that method in such a way that the memory usage is O(1) instead of O(N).