主要区别在于,第一个允许您根据InputStream接口的实现仅从二进制源读取XML内容。即:直接来自文件(使用FileInputStream),打开的套接字(来自Socket.getInputStream())等。
第二个字符DocumentBuilder.parse(InputSource)允许您也从二进制源(这是一个InputStreamimpl)和字符源(Reader实现)中读取数据。因此,通过此代码,您可以使用XML字符串(使用StringReader)或BufferedReader。
虽然使用第二种方法已经可以处理InputStreams,但是第一种方法是一种快捷方式,因此,有了第二种方法时,您InputStream无需创建新方法InputSource。实际上,该InputStream方法的源代码是:
public Document parse(InputStream is)
throws SAXException, IOException {
if (is == null) {
throw new IllegalArgumentException("InputStream cannot be null");
}
InputSource in = new InputSource(is);
return parse(in);
}
Run Code Online (Sandbox Code Playgroud)