0

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
  • Are you wanting to parse the same XML file twice? You haven't told us anything about the parser you're using, so we have no way of knowing whether it can accept input in any format other than a file. Commented Jan 6, 2010 at 5:59
  • 1
    maybe it's just a typo in your pasted code, but reader1 is not the same as reader, you should be getting a compile error... Commented Jan 6, 2010 at 6:00
  • 1
    (It's worth noting that XML includes a charset declaration, so a String of an XML file is not something you should be parsing. Keep it as a sequence of octets.) Commented Jan 6, 2010 at 14:41

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.