2012-11-09 3 views
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; 
} 

답변

2

, 다음을 시도하십시오

대신

exceptions.add(exception); 

의 수행을 다음

throw exception; 

귀하의 구현이 소모 될 수 있습니다 검증 예외. exceptions을 확인하고 그 내용을 기반으로 반환 할 수도 있습니다. 나중에 사용하지 않는 목록을 작성하는 이유는 무엇입니까?

관련 문제