18

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

4

1 回答 1

45

你不转换,你包装它。

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

于 2013-03-04T14:23:18.120 回答