You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Kavita Ashok Sukerkar <ks...@engin.umich.edu> on 2003/07/02 16:53:00 UTC

DOMParser exceptions

Hi everyone,

  I am trying to use the Xerces parsers for xml schema validation within
Java code.  I use the parse function from the XMLParser class, that
DOMParser inherits from.  This function throws two kinds of exceptions:
IOException and SAXException.  But what if the xml file I am supplying is
well formed and NOT valid? DOes it throw any kind of exception?  My
program just outputs an error message saying that the file was not valid.
I want my function to be able to return a 0 if the parsing was
unsuccessful, but I can't do that if there are just error messages being
outputted.  This is my code.....can someone help me please!


import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.parsers.*;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.io.IOException;

public class DOMParserDemo
{
     private static void setFeature(DOMParser parser, String feature,
                                              boolean setting)
     {
         try
         {
         	parser.setFeature(feature, setting);
         }
         catch (SAXNotRecognizedException e)
         {
            System.out.print("Unrecognized feature: ");
            System.out.println(feature);
         }
         catch (SAXNotSupportedException e)
         {
            System.out.print("Unrecognized feature: ");
            System.out.println(feature);
         }
     }


     public static void main (String args[])
     {
         DOMParser parser = null;
         parser = new DOMParser();

         Document doc = null;

         setFeature(parser, "http://xml.org/sax/features/validation",
                          true);
         setFeature(parser,
          "http://apache.org/xml/features/validation/schema",true);
         setFeature(parser,
      "http://apache.org/xml/features/validation/schema-full-checking", true);

         try
         {
            parser.parse("C:/Program Files/Xinox Software/JCreator
                     LE/MyProjects/soaptest/XsuTest/SQRT.xml");
         }
         catch (IOException ie)
         {
            System.out.println("Could not read file.");
         }
         catch (SAXException e)
         {
            System.out.print("Could not create Document: ");
            System.out.println(e.getMessage());
         }
         catch (Exception e)
         {
         	System.out.print("Error occurred\n");
      	 }

         System.out.println("done\n");
    }
}

Thanks,
Kavita


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


Re: DOMParser exceptions

Posted by "K. Venugopal" <k....@sun.com>.
Hi Kavita ,


 


Kavita Ashok Sukerkar wrote:

>Hi everyone,
>
>  I am trying to use the Xerces parsers for xml schema validation within
>Java code.  I use the parse function from the XMLParser class, that
>DOMParser inherits from.  This function throws two kinds of exceptions:
>IOException and SAXException.  But what if the xml file I am supplying is
>well formed and NOT valid? DOes it throw any kind of exception?  My
>program just outputs an error message saying that the file was not valid.
>I want my function to be able to return a 0 if the parsing was
>unsuccessful,
>
xerces-j-user@xml.apache.org is the list for user related queries,
One way is to write a small function that returns boolean value 
appropriately when errors/warnings .. are reported, register a error 
handler to identify them. Refer to sample programs provided by xerces 
for more help on error handlers.

Hope this helps.

Regards,
venu





> but I can't do that if there are just error messages being
>outputted.  This is my code.....can someone help me please!
>
>
>import org.apache.xerces.parsers.DOMParser;
>import org.apache.xerces.parsers.*;
>import org.w3c.dom.Document;
>import org.xml.sax.SAXException;
>import org.xml.sax.SAXNotRecognizedException;
>import org.xml.sax.SAXNotSupportedException;
>import java.io.IOException;
>
>public class DOMParserDemo
>{
>     private static void setFeature(DOMParser parser, String feature,
>                                              boolean setting)
>     {
>         try
>         {
>         	parser.setFeature(feature, setting);
>         }
>         catch (SAXNotRecognizedException e)
>         {
>            System.out.print("Unrecognized feature: ");
>            System.out.println(feature);
>         }
>         catch (SAXNotSupportedException e)
>         {
>            System.out.print("Unrecognized feature: ");
>            System.out.println(feature);
>         }
>     }
>
>
>     public static void main (String args[])
>     {
>         DOMParser parser = null;
>         parser = new DOMParser();
>
>         Document doc = null;
>
>         setFeature(parser, "http://xml.org/sax/features/validation",
>                          true);
>         setFeature(parser,
>          "http://apache.org/xml/features/validation/schema",true);
>         setFeature(parser,
>      "http://apache.org/xml/features/validation/schema-full-checking", true);
>
>         try
>         {
>            parser.parse("C:/Program Files/Xinox Software/JCreator
>                     LE/MyProjects/soaptest/XsuTest/SQRT.xml");
>         }
>         catch (IOException ie)
>         {
>            System.out.println("Could not read file.");
>         }
>         catch (SAXException e)
>         {
>            System.out.print("Could not create Document: ");
>            System.out.println(e.getMessage());
>         }
>         catch (Exception e)
>         {
>         	System.out.print("Error occurred\n");
>      	 }
>
>         System.out.println("done\n");
>    }
>}
>
>Thanks,
>Kavita
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
>
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org