pL4*_*u33 2 java xml validation xsd inputstream
我有一个从 xml 文件中获取输入流的网络服务。现在,我想验证并使用相同的输入流读取它。我使用标记并重置它。
在 Glassfish 和 Websphere 上它工作正常。但是当我使用 openEJB 运行我的集成测试时,流将在验证后关闭。我可以在一个简单的例子中重现它。
我怎样才能更好地意识到它?验证器实现始终相同。但是每个环境都使用输入流的另一种实现。
public class XMLReader {
public static void main(String[] args) {
try {
XMLReader reader = new XMLReader();
InputStream stream = new BufferedInputStream(new FileInputStream(
new File("myXML.xml")));
reader.read(stream);
} catch (Exception e) {
e.printStackTrace();
}
}
public void read(InputStream xmlInputStream) throws SAXException,
IOException {
if (xmlInputStream.markSupported()) {
xmlInputStream.mark(0);
validateXML(xmlInputStream);
xmlInputStream.reset();
readXML(xmlInputStream);
}
}
private void readXML(InputStream xmlInputStream) {
// READ xmInputStream with STAX, JAXB, etc. whatever
}
private void validateXML(InputStream xmlInputStream) throws SAXException,
IOException {
Source schemaFile = new StreamSource(new File("myXSD.xsd"));
Source xmlFile = new StreamSource(xmlInputStream);
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
try {
validator.validate(xmlFile);
System.out.println("is valid");
} catch (SAXException e) {
System.out.println("is NOT valid");
System.out.println("Reason: " + e.getLocalizedMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
例外:
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:145)
at java.io.BufferedInputStream.reset(BufferedInputStream.java:414)
at xmltest.XMLReader.read(XMLReader.java:36)
at xmltest.XMLReader.main(XMLReader.java:27)
Run Code Online (Sandbox Code Playgroud)
小智 5
不幸的是,XML 解析器在完成读取后关闭流。我不明白为什么它真的会这样做,我不建议任何人关闭他们不拥有的流。可能有一个原因,我无法那么快地掌握。
无论如何,您可以做的是拥有一个 BufferedInputStream,它不会关闭包装的 InputSteam:
public static void main(String[] args) {
try (FileInputStream in = new FileInputStream(new File("myXML.xml"))){
XMLReader reader = new XMLReader();
InputStream stream = new BufferedInputStream(in) {
@Override
public void close() throws IOException {
// don't close
}
};
reader.read(stream);
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5267 次 |
| 最近记录: |