有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    InputSource可以从InputStream读取,但它也可以从Reader读取或直接从URL读取(打开流本身)。从InputStream解析等同于从new InputSource(theStream)解析

    如果要解析的文件通过相对URI引用了外部DTD或任何外部实体,那么就不能从普通的InputStream解析它,因为解析器不知道解析这些相对路径时应该使用的基本URL。在这种情况下,您需要从流中构造一个InputSource,并使用setSystemId设置基本URI,然后从该源解析,而不是简单地将流直接传递给解析器