You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by KEVIN LANDRY <ke...@alcatel.com> on 2001/11/22 22:38:16 UTC

Validation activation problems.

                 I can't seem to activate validation on this Xerces
SAXParser!

                 I added to code to set the feature to true and set the
ErrorHandler too.
                 Anyone know why it doesn't work?

                 My code is below. My application compiles and runs, but
still doesn't validate.

-----------------

                 SAXParserFactory factory = null;
                 try {
                 factory = SAXParserFactory.newInstance();
                 } catch (FactoryConfigurationError e) {
                 //TBD Error:
                 }

                 factory.setValidating(true);

                 SAXParser parser = null;
                 try {
                 parser = factory.newSAXParser();
                 } catch (Exception e) {
                 //TBD Error:
                 }

                 if (parser == null) {
                 //TBD Error:
                 }

                 if (!parser.isValidating()) {
                 //TBD Error:
                 }

                 try {

parser.getXMLReader().setFeature("http://xml.org/sax/features/validation",

                 true);
                 ErrorHandler errorHandler = new ErrorHandler() {
                 public void warning(SAXParseException exception)
                 throws SAXException
                 {
                 throw exception;
                 }
                 public void error(SAXParseException exception)
                 throws SAXException
                 {
                 throw exception;
                 }
                 public void fatalError(SAXParseException exception)
                 throws SAXException
                 {
                 throw exception;
                 }
                 };
                 parser.getXMLReader().setErrorHandler(errorHandler);
                 } catch (Exception e) {
                 //TBD Error:
                 }

---------------------

Any help would be much appreciated.  Thanks in advance.

                 Regards,
                 KL

Re: Validation activation problems.

Posted by KEVIN LANDRY <ke...@alcatel.com>.
I figured it out!  The problem had to do with the way I was calling the parse
method.

I was doing this:

parser.parse(file, handler);

when I should have been doing this:

parser.getXMLReader().parse(filePathName);

Because I didn't call the parse method from the SAXParser's underlying
XMLReader, the parse didn't know the validation feature was on and thus didn't
validation my file!

So if anyone ever comes across this problem I was having, just make sure
you're looks something like this and you should be okay:

        String featureName = "http://xml.org/sax/features/validation";
        String filePathName = "C:\xml\example.xml"
        boolean validate = true;

        SAXParserFactory factory = null;
        try {
          factory = SAXParserFactory.newInstance();
        } catch (FactoryConfigurationError e) {
           // Print Error message
           // exit
        }

        factory.setValidating(validate);

        SAXParser parser = null;
        try {
          parser = factory.newSAXParser();
        } catch (Exception e) {
          //Print Error message
          //exit
        }

        if (parser == null) {
          //Print Error message
          //exit
        }

        XMLReader reader = null;
        try {
          reader = parser.getXMLReader();
          reader.setFeature(featureName, validate);
          reader.setErrorHandler(new MyErrorHandler);
          reader.setContentHandler(new MyContentHandler());
          reader.parse(filePathName);
        } catch (Exception e) {
          //Print Error message
        }

Thanks to those who replied!

Cheers,
KL

KEVIN LANDRY wrote:

>                  I can't seem to activate validation on this Xerces
> SAXParser!
>
>                  I added to code to set the feature to true and set the
> ErrorHandler too.
>                  Anyone know why it doesn't work?
>
>                  My code is below. My application compiles and runs, but
> still doesn't validate.
>
> -----------------
>
>                  SAXParserFactory factory = null;
>                  try {
>                  factory = SAXParserFactory.newInstance();
>                  } catch (FactoryConfigurationError e) {
>                  //TBD Error:
>                  }
>
>                  factory.setValidating(true);
>
>                  SAXParser parser = null;
>                  try {
>                  parser = factory.newSAXParser();
>                  } catch (Exception e) {
>                  //TBD Error:
>                  }
>
>                  if (parser == null) {
>                  //TBD Error:
>                  }
>
>                  if (!parser.isValidating()) {
>                  //TBD Error:
>                  }
>
>                  try {
>
> parser.getXMLReader().setFeature("http://xml.org/sax/features/validation",
>
>                  true);
>                  ErrorHandler errorHandler = new ErrorHandler() {
>                  public void warning(SAXParseException exception)
>                  throws SAXException
>                  {
>                  throw exception;
>                  }
>                  public void error(SAXParseException exception)
>                  throws SAXException
>                  {
>                  throw exception;
>                  }
>                  public void fatalError(SAXParseException exception)
>                  throws SAXException
>                  {
>                  throw exception;
>                  }
>                  };
>                  parser.getXMLReader().setErrorHandler(errorHandler);
>                  } catch (Exception e) {
>                  //TBD Error:
>                  }
>
> ---------------------
>
> Any help would be much appreciated.  Thanks in advance.
>
>                  Regards,
>                  KL
>
>   ------------------------------------------------------------------------
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org