Processing two XMLStreamReader2 object from same input stream, will closeCompletely() on one reader object affect another one?

19 views
Skip to first unread message

snehal.sh...@gmail.com

unread,
Feb 6, 2020, 8:24:11 AM2/6/20
to Woodstox User Mailing List
Hi Team,

I am using org.codehaus.stax2.XMLStreamReader2 to read from a xml file. While reading it i need two different objects of XMLStreamReader2 for same xml file.

When i use CloseCompletely() method as mentioned in http://fasterxml.github.io/stax2-api/javadoc/3.1.4/org/codehaus/stax2/XMLStreamReader2.html#closeCompletely() link, It says  this method also does close the underlying input source.
I want to ask here, if i use two reader objects from same input source, calling closeCompletely() method affect other reader object, or it will close refferences for reader1 object only?

PFB the sample code for the same:

XMLStreamReader2 reader1 = (XMLStreamReader2) inputFactory.createXMLStreamReader(new ByteArrayInputStream(byteArrayFromInputStream));
reader1.next();

XMLStreamReader2 reader2 = (XMLStreamReader2) inputFactory.createXMLStreamReader(new ByteArrayInputStream(byteArrayFromInputStream));
reader2.next();

 //here do some processing and close the reader2
reader2.closeCompletely();

//When i call closeCompletely() method at this point , the cursor of reader1 object is at some xml element tag. Will this closeCompletely() closes all references of inputsources? so that when i do reader1.next() will it work without any error?
//If yes what is the best way to close one reader object safely without affecting other if both the reader objects are from same inputsource?

.
reader1.next(); // will this work properly and i can continue for reading next elements?


Thanks,
Snehal

Tatu Saloranta

unread,
Feb 6, 2020, 1:45:22 PM2/6/20
to snehal.sh...@gmail.com, Woodstox User Mailing List
On Thu, Feb 6, 2020 at 5:24 AM <snehal.sh...@gmail.com> wrote:
Hi Team,

I am using org.codehaus.stax2.XMLStreamReader2 to read from a xml file. While reading it i need two different objects of XMLStreamReader2 for same xml file.

When i use CloseCompletely() method as mentioned in http://fasterxml.github.io/stax2-api/javadoc/3.1.4/org/codehaus/stax2/XMLStreamReader2.html#closeCompletely() link, It says  this method also does close the underlying input source.
I want to ask here, if i use two reader objects from same input source, calling closeCompletely() method affect other reader object, or it will close refferences for reader1 object only?

It will close the source you give if that source is closable: if you give `InputStream` or `Reader` that stream or reader. If input source is not, Woodstox has to create stream or reader, and will create that object.

So....


PFB the sample code for the same:

XMLStreamReader2 reader1 = (XMLStreamReader2) inputFactory.createXMLStreamReader(new ByteArrayInputStream(byteArrayFromInputStream));
reader1.next();

XMLStreamReader2 reader2 = (XMLStreamReader2) inputFactory.createXMLStreamReader(new ByteArrayInputStream(byteArrayFromInputStream));
reader2.next();

 //here do some processing and close the reader2
reader2.closeCompletely();

//When i call closeCompletely() method at this point , the cursor of reader1 object is at some xml element tag. Will this closeCompletely() closes all references of inputsources? so that when i do reader1.next() will it work without any error?
//If yes what is the best way to close one reader object safely without affecting other if both the reader objects are from same inputsource?

.
reader1.next(); // will this work properly and i can continue for reading next elements?


Yes, because you have 2 different InputStreams, they are not directly related in any way.

If you tried to create just one ByteArrayInputStream, pass that, it would not work -- but that would make no sense since everything else would be joined too (reads from stream reader).

I hope this helps,

-+ Tatu +-
 

Thanks,
Snehal

--
You received this message because you are subscribed to the Google Groups "Woodstox User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to woodstox-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/woodstox-user/4c9ff0ca-2fb3-4d62-9aa1-a64a994466ba%40googlegroups.com.

snehal.sh...@gmail.com

unread,
Feb 7, 2020, 3:16:37 AM2/7/20
to Woodstox User Mailing List
Thank you very much for the help. It helped me a lot.

Thanks,
Snehal
Reply all
Reply to author
Forward
0 new messages