java InputSource和InputStream之间有什么区别?
在解析xml时使用InputSource和InputStream有什么区别。 我在一些教程中看到了这两个示例
无输入源:
InputStream is;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbFactory.newDocumentBuilder();
Document document = db.parse(is);
对于InputSource,区别在哪里
DocumentBuilder db = dbFactory.newDocumentBuilder();
InputSource inputSource = new InputSource(is);
Document document = db.parse(inputSource);
那么在性能上有什么不同吗?还是别的什么
# 1 楼答案
InputSource
可以从InputStream
读取,但它也可以从Reader
读取或直接从URL读取(打开流本身)。从InputStream
解析等同于从new InputSource(theStream)
解析如果要解析的文件通过相对URI引用了外部DTD或任何外部实体,那么就不能从普通的
InputStream
解析它,因为解析器不知道解析这些相对路径时应该使用的基本URL。在这种情况下,您需要从流中构造一个InputSource
,并使用setSystemId
设置基本URI,然后从该源解析,而不是简单地将流直接传递给解析器