4 Replies Latest reply: Jun 5, 2007 9:44 AM by 807600 RSS

    all validation error from saxParser.parse

    807600
      I am getting just the 1st error but I want all the errors in the 1st pass itself.

      Attaching the code for reference also.
      ?would really appreciate you suggestion.
      ==code---

      public class EDITExtractSchema {

      static Document document;

      public void validateXMLVOusingXSD(String str)
      {

      try {
      //create a SchemaFactory capable of understanding W3C XML Schemas (WXS)
      SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
      //Set the error handler to receive any error during Schema Compilation
      XMLErrorHandler errorHandler = new XMLErrorHandler();
      factory.setErrorHandler(errorHandler);

      //set the resource resolver to customize resource resolution
      //factory.setResourceResolver( new MyLSResourceResolver());
      // load a WXS schema, represented by a Schema instance
      File schemaFile = new File("C:/Temp/project2-CorrVO/XSDs/TaxExtract5498DetailVO.xsd");
      Schema schema = factory.newSchema(new StreamSource(schemaFile));
      SAXParserFactory spf = SAXParserFactory.newInstance();
      //spf.setNamespaceAware(true);
      //spf.setValidating(true);
      spf.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);


      //Just set the Schema instance on SAXParserFactory
      spf.setSchema(schema);

      //Obtain the SAXParser instance
      SAXParser saxParser = spf.newSAXParser();

      //parser will parse the XML document but validate it using Schema instance
      //saxParser.parse(new File(str), myHandler);
      saxParser.parse(new File(str),new MyDefaulyHandler());
      }catch(ParserConfigurationException e) {
      System.out.println("ParserConfigurationException-" + e);
      }catch (SAXException e) {
      System.out.println("SAXException-" + e);
      }
      catch (Exception e) {
      System.out.println("Exception-" + e);
      }






      /*// output the errors XML
      XMLWriter writer = null;
      try {
      writer = new XMLWriter(OutputFormat.createPrettyPrint());
      } catch (UnsupportedEncodingException e) {
      System.out.println("validate error" + e);
      e.printStackTrace();
      }
      try {
      writer.write(errorHandler.geterrors());
      } catch (IOException e) {
      System.out.println("validate error" + e);
      e.printStackTrace();
      }
      */


      }

      //implement error handler
      public static class XMLErrorHandler implements ErrorHandler {

      private boolean valid = true;

      public void reset() {
      // Assume document is valid until proven otherwise
      valid = true;
      }

      public boolean isValid() {
      return valid;
      }

      public void warning(SAXParseException exception) throws SAXException{


      System.out.println("Warning: " + exception.getMessage());
      System.out.println(" at line " + exception.getLineNumber()
      + ", column " + exception.getColumnNumber());
      System.out.println(" in entity " + exception.getSystemId());
      valid = true;

      }


      public void error(SAXParseException exception) throws SAXException{


      System.out.println("Error: " + exception.getMessage());
      System.out.println(" at line " + exception.getLineNumber()
      + ", column " + exception.getColumnNumber());
      System.out.println(" in entity " + exception.getSystemId());
      valid = true;
      /* // output the errors XML
      XMLWriter writer = null;
      try {
      writer = new XMLWriter(OutputFormat.createPrettyPrint());
      } catch (UnsupportedEncodingException e) {
      System.out.println("validate error" + e);
      e.printStackTrace();
      }
      try {
      writer.write((Object)exception);
      } catch (IOException e) {
      System.out.println("validate error" + e);
      e.printStackTrace();
      }
      */

      }


      public void fatalError(SAXParseException exception) throws SAXException {


      System.out.println("Fatal Error: " + exception.getMessage());
      System.out.println(" at line " + exception.getLineNumber()
      + ", column " + exception.getColumnNumber());
      System.out.println(" in entity " + exception.getSystemId());
      valid = true;

      }
      }

      public static class MyDefaulyHandler extends DefaultHandler {

      private boolean valid = true;

      public void reset() {
      // Assume document is valid until proven otherwise
      valid = true;
      }

      public boolean isValid() {
      return valid;
      }

      public void warning(SAXParseException exception) throws SAXException{


      System.out.println("Warning: " + exception.getMessage());
      System.out.println(" at line " + exception.getLineNumber()
      + ", column " + exception.getColumnNumber());
      System.out.println(" in entity " + exception.getSystemId());
      valid = true;

      }


      public void error(SAXParseException exception) throws SAXException{


      System.out.println("Error: " + exception.getMessage());
      System.out.println(" at line " + exception.getLineNumber()
      + ", column " + exception.getColumnNumber());
      System.out.println(" in entity " + exception.getSystemId());
      valid = true;
      /* // output the errors XML
      XMLWriter writer = null;
      try {
      writer = new XMLWriter(OutputFormat.createPrettyPrint());
      } catch (UnsupportedEncodingException e) {
      System.out.println("validate error" + e);
      e.printStackTrace();
      }
      try {
      writer.write((Object)exception);
      } catch (IOException e) {
      System.out.println("validate error" + e);
      e.printStackTrace();
      }
      */

      }


      public void fatalError(SAXParseException exception) throws SAXException {


      System.out.println("Fatal Error: " + exception.getMessage());
      System.out.println(" at line " + exception.getLineNumber()
      + ", column " + exception.getColumnNumber());
      System.out.println(" in entity " + exception.getSystemId());
      valid = true;

      }
      }



      public static void main(String[] args) {

      EDITExtractSchema schemaClass = new EDITExtractSchema();
      try {
      schemaClass.validateXMLVOusingXSD("C:/Temp/project2-CorrVO/XSDs/TaxExtract5498DetailVO.xml");
      }catch (Exception e) {
      System.out.println("xml file read error Exception-" + e);
      }
      // */

      }


      }

      --====
      Regards,
      Tapas