Convert String into InputStream

In the last days I was asked several times: “How do I convert a string into an InputStream, so that I can parse my XML-String with the SAX parser?” (note: or any other XML-Parser like DOM, JAXP, JDOM, …).

Being tired of explaining the same thing again and again – here is the answer:

1
2
3
4
5
6
7
String myString = 'content of your very own string';
ByteArrayInputStream in = new ByteArrayInputStream(myString.getBytes());
InputSource is = new InputSource();
is.setByteStream(in);
 
Parser myParser = new Parser();
myParser.parse(is);

Quick & Easy :)

If you enjoyed this post, make sure you subscribe to my RSS feed!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Get in contact:

Categories