7

What is the difference between using InputSource and InputStream when I am parsing xml. I saw both examples in some tutorials

without InputSource:

InputStream is;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbFactory.newDocumentBuilder();
Document document = db.parse(is);

and with InputSource, where is the difference

DocumentBuilder db = dbFactory.newDocumentBuilder();
InputSource inputSource = new InputSource(is);
Document document = db.parse(inputSource);

So is there any difference in performance? Or in something else?

smn.tino
  • 2,272
  • 4
  • 32
  • 41
Pauli
  • 538
  • 8
  • 22
  • did you google your question? "difference between InputSource and InputStream ?" second answer ... – DavidPostill Aug 21 '14 at 15:36
  • ye I did, sorry don't know why, I haven't found [this](http://stackoverflow.com/questions/4253533/java-jaxp-xml-parsing-differences-of-documentbuilder) question – Pauli Aug 21 '14 at 15:44

1 Answers1

9

An InputSource can read from an InputStream, but it can also read from a Reader or direct from a URL (opening the stream itself). Parsing from an InputStream is equivalent to parsing from a new InputSource(theStream).

If the file you want to parse makes reference to an external DTD or any external entities by relative URIs then you can't parse it from a plain InputStream, as the parser doesn't know the base URL it should use to resolve these relative paths. In that case you will need to construct an InputSource from the stream and use setSystemId to set the base URI and then parse from that source, rather than simply passing the stream direct to the parser.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • 1
    Thanks for more information :) What I was looking for I found [here](http://stackoverflow.com/questions/4253533/java-jaxp-xml-parsing-differences-of-documentbuilder) – Pauli Aug 21 '14 at 15:51