StreamSource:InputStream vs FileInputStream

时间:2014-03-18 15:23:59

标签: java xml xsd inputstream

使用时

ZipArchiveEntry schemaEntry = zipFile.getEntry("metadata.xml");
InputStream schemaIS = zipFile.getInputStream(schemaEntry);
Schema schema = factory.newSchema(new StreamSource(schemaIS));

我得到了

s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than     'xs:appinfo' and 'xs:documentation'. Saw 'archive'.

但是在使用时

File schemaFile = new File("/somedir/schema.xsd");
InputStream schemaFileIS = new FileInputStream(schemaFile);
Schema schema = factory.newSchema(new StreamSource(schemaFileIS));

它正常工作,即:它显示模式错误。

我想要验证的.xml文件也是如此。如果我从File创建一个StreamSource就可以了。但是如果我使用InputStream(来自zip)它只是跳过验证(不会抛出异常,它应该)。

因此,如果我使用StreamSource(FileInputStream),则正确加载.xsd和.xml。但是,如果我使用InputStream(我必须使用它),它就行不通。

编辑: 改变了

ZipArchiveEntry schemaEntry = zipFile.getEntry("metadata.xml");

ZipArchiveEntry schemaEntry = zipFile.getEntry("metadata.xsd");

它正在发挥作用。感谢@fge replier。

0 个答案:

没有答案