0
尋找確認下面是根據模式驗證XML字符串的最有效的Java解決方案。任何其他更有效的記憶或性能方式?根據XSD驗證XML
private boolean isXMLValid(String XSDPath, String XML) {
final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
final Schema schema = factory.newSchema(new File(XSDPath));
final Validator validator = schema.newValidator();
validator.validate(new StreamSource(new ByteArrayInputStream(XML.getBytes())));
} catch (IOException | SAXException e) {
System.out.println("Exception: " + e.getMessage());
return false;
}
return true;
}
僅針對某些情況,此驗證位於REST API的後面,將由另一個應用程序調用以驗證其XML,因此我不確定是否可以獲得更多流暢性,但是如果您有任何想法,請告訴我。謝謝! – c12
'ServletRequest'有一個['getInputStream'](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getInputStream())方法。 – lexicore