0
다음 유효성 검사 코드를 Java로 작성했지만 항상 true를 반환합니다. XML을 수정하고 XSD와 호환되지 않는 경우에도 여전히 true를 반환합니다. 이것을 봐주세요. 당신의 ErrorHandler
인터페이스 구현에XSD에 대한 XML 유효성 검사 중에 오류가 발생했습니다
public static boolean isValidXML(String filePath,String xsdFile){
boolean bValue = true;
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
Document document = parser.parse(new File(filePath));
Schema schema = schemaFactory.newSchema(new File(xsdFile));
Validator validator = schema.newValidator();
final List<SAXParseException> exceptions = new LinkedList<SAXParseException>();
validator.setErrorHandler(new ErrorHandler()
{
public void warning(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
public void fatalError(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
public void error(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
});
validator.validate(new DOMSource(document));
} catch (SAXException e) {
bValue = false;
logger.error("Error while Validating the XML."+e);
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bValue;
}