Home > Java > Convert String into InputStream

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!

Post to Twitter Tweet This Post

Categories: Java Tags: , , , , ,
  1. No comments yet.
  1. No trackbacks yet.