54

I am following a guide, and it gives me the following code:

InputSource inputSource = new InputSource(new FileInputStream(new File("/path/to/xml/file.xml"))));

What I would like to know, is how I can still create an org.xml.sax.InputSource, but instead of reading the content of a file, use a String variable that I already have.

Kamil Naja
  • 6,267
  • 6
  • 33
  • 47
Chiggins
  • 8,197
  • 22
  • 56
  • 81

2 Answers2

100

Use a StringReader instead of a FileInputStream.

See the documentation for StringReader

example:

InputSource inputSource = new InputSource( new StringReader( myString ) );
Oskar Birkne
  • 803
  • 7
  • 18
Gavin H
  • 10,274
  • 3
  • 35
  • 42
4

InputSource inputSource = new InputSource(new java.io.StringReader(string)); if it is org.xml.sax.InputSource.

khachik
  • 28,112
  • 9
  • 59
  • 94