如何将输入流转换为源?

2022-09-02 11:44:47

我想使用存储在设备上的 xsd 架构文件验证 xml 文档。以下是我的示例代码:

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// schema file on my device
InputStream isSchema = context.getResources().openRawResource(xsd_file);
// InputStream => Source conversion
Source schemaSource = ????
Schema schema = factory.newSchema(schemaSource);

Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));

问题:如何将 InputStream 转换为 SchemaFactory::newSchema 方法所需的源代码?


答案 1

你不转换,你包装它。

Source schemaSource = new StreamSource(isSchema);

请参见: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/transform/stream/StreamSource.html#StreamSource%28java.io.InputStream%29


答案 2

推荐