有什么区别吗
DocumentBuilder.parse(InputStream)和DocumentBuilder.parse(InputSource)?我只能发现,对于第一种情况,解析器从流中检测编码,因此更安全,而在后一种情况下,我不确定是否需要设置编码。
还有什么其他的问题(比如表现)我应该知道?
发布于 2010-11-23 15:18:35
主要区别在于,第一种方法允许您根据InputStream接口的实现只从二进制源读取XML内容。直接来自文件(使用FileInputStream)、打开的套接字(来自Socket.getInputStream())等。
第二种方法是DocumentBuilder.parse(InputSource),它也允许您从二进制源(即InputStream impl) 和从字符源(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);
}https://stackoverflow.com/questions/4253533
复制相似问题