tags:

views:

62

answers:

1

I have XML data as a string which has to parsed, I am converting the XML string to inputsource using the following code:

StringReader reader1 = new StringReader( xmlstring);
InputSource inputSource1= new InputSource( reader );

And I am passing input source to

Document doc = builder.build(inputSource);

I want to use the same inputSource1 in another parser class also, but I am getting stream closed.

How would I handle this or is there any other way to pass XML data to a parser other than file?

+3  A: 

Looking at the JavaDoc, it seems that InputSource is not designed to be shared and reused by multiple parsers.

standard processing of both byte and character streams is to close them on as part of end-of-parse cleanup, so applications should not attempt to re-use such streams after they have been handed to a parser.

So you will have to create a new InputSource. If you are really reading from a String, there would be no difference in I/O or memory cost anyway.

Thilo
Your right inputsource cannot be reused,i chanded the usage of inputsource to ByteArrayInputStream in = new ByteArrayInputStream(inputSource.getBytes()); InputSource is = new InputSource(); is.setByteStream(in);passing "is " to parser its working fine
sarah