如何将InputStream转换为源?

时间:2013-03-04 14:08:55

标签: java android validation xsd

我想使用存储在我设备上的 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方法所需的Source?

1 个答案:

答案 0 :(得分:36)

你没有转换,你包装它。

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