java 如何将 InputStream 转换为 Source?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15203439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert InputStream into Source?
提问by tommyk
I want to validate xmldocument using xsdschema file stored on my device. Here is my example code:
我想使用存储在我设备上的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));
Question:How can I convert InputStream into Source required by SchemaFactory::newSchema method ?
问题:如何将 InputStream 转换为 SchemaFactory::newSchema 方法所需的 Source?
回答by Joeri Hendrickx
You don't convert, you wrap it.
你不转换,你包装它。
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