Hi friends,
I’m facing problem in validating xml file against a schema with Xerces 2.9.0. I 
am new to this xml validation field.
My Problem is my DOM Parser do not log all the well Formed errors. It only log 
one fatal error that is first from the bottom of the xml file. If I introduced 
two well formed error then it only reports last error(first from the bottom of 
the xml doc)But I need it to log all the fatal errors. When there is one fatal 
error (well formed error) in the xml file then it only reports simple errors 
like data type mismatch that occurs after source of the fatal error. Setting 
feature continue-after-fatal –error did not work.
The (relevant portion of the) XML document follows:***************
<?xml version="1.0" encoding="UTF-8"?>
<rootDirMapping>
        <rootDir>AppData</rootDir>
        <mappingFile>MappingFiles</mappingFile>
        <schemaFile>SchemaFiles</schemaFile>
        <validationHierarchy>1hierarchy</validationHierarchy>
        <database>database</database>
</rootDirMapping>
The (relevant portion of the) XML schema follows:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
xmlns:xs="http://www.w3.org/2001/XMLSchema";>
        <xs:element name="rootDirMapping">
                <xs:complexType>
                        <xs:sequence>
                                <xs:element name="rootDir" type="xs:string" />
                                <xs:element name="mappingFile" 
type="xs:integer" />
                                <xs:element name="schemaFile" type="xs:integer" 
/>
                                <xs:element name="validationHierarchy" 
type="xs:string" />
                                <xs:element name="database" type="xs:integer" />
                        </xs:sequence>
                </xs:complexType>
        </xs:element>
</xs:schema>
************************************************************* 
The (relevant portion of the) Java Code follows:
public static boolean validateWithSchema(String XMLFile, String Schema){
                 boolean result = true;
                 long startTime = System.currentTimeMillis();
                 // Instantiate the DOM parser.
                 DOMParser parser = new DOMParser();
                 // set the features
                 try{
                 
parser.setFeature("http://xml.org/sax/features/namespaces",true);
                 
parser.setFeature("http://xml.org/sax/features/validation",true);
                 
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
                 
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
                 
parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error";, 
true);
                 
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",Schema);
                 // myErrorHandler is a descendant of ErrorHandler, it should 
be set here to be able to catch parse errors
                 forgivingErrorHandler hndl = new forgivingErrorHandler();
                 parser.setErrorHandler(hndl);
                 } catch (SAXNotSupportedException ex){
                         System.out.println("SAXNotSupportedException 
Exception");
                 } catch (SAXNotRecognizedException ex){
                         System.out.println("SAXNotRecognizedException 
Exception");
                 }
                 // parse the xml file, the errorhandler class has callbacks,
                 // so those will be called automatically there is a parse error
                 try{
                         parser.parse(XMLFile);
                     //System.out.println("Parsed Successfully by DOM Parser");
                 } catch (org.xml.sax.SAXException ex){
                         System.out.println("SAXException Exception");
                     ex.printStackTrace();
                 } catch (java.io.IOException ex){
                         System.out.println("IOException Exception");
                 }
                return result;
                }
The (relevant portion of the) Java Code for Error Handler follows:
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class forgivingErrorHandler implements org.xml.sax.ErrorHandler {
        
        public forgivingErrorHandler() {}
        public void error(org.xml.sax.SAXParseException err) throws 
SAXException {
                FileWriter fstream;
                try {
                        String 
fs="D:\\WORKSPACE\\xmlValidation\\ExceptionLogs\\Errors.txt";
                        fstream = new FileWriter(fs,true);
                        PrintWriter out = new PrintWriter(fstream);
                        out.write("Exception 
at:"+err.getLineNumber()+"***"+err.getMessage()+"\n\n\n");
                        out.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                
        }

        public void fatalError(org.xml.sax.SAXParseException fe) throws 
SAXException {
                FileWriter fstream;
                try {
                        String 
fs="D:\\WORKSPACE\\xmlValidation\\ExceptionLogs\\FatalErrors.txt";
                        fstream = new FileWriter(fs,true);
                        PrintWriter out = new PrintWriter(fstream);
                        out.write("Exception 
at:"+fe.getLineNumber()+"***"+fe.getMessage()+"\n\n\n");
                        out.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                
        }

        public void warning(org.xml.sax.SAXParseException war) throws 
SAXException {
                FileWriter fstream;
                try {
                        fstream = new 
FileWriter("D:\\WORKSPACE\\xmlValidation\\ExceptionLogs\\Warnings.txt",true);
                        PrintWriter out = new PrintWriter(fstream);
                        out.write("Exception 
at:"+war.getLineNumber()+"***"+war.getMessage()+"\n\n\n");
                        out.close();
                } catch (IOException e) {
                        System.out.println("Here are we in the Warning 
section....");
                        e.printStackTrace();
                }
                
        }
        
        
}
Unfortunately, when I try to validate the above XML document against the 
Schema, it throws errors as I explained above… only one fatal error som times 
errors those are occurred after the fatal error(before the fatal error from 
bottom.)

After several long searches in the web, I could not find any solution.
Please help me or please let me know where to find an answer.
Kind regards and please apologize if this question already appeared in the 
mailing list, but -- again -- all I found/read about this problem didn't 
suffice.


Vineet Rajput
Software Engineer 
Snap-On Business Solution
Logix Techno Park,
Noida,INDIA

Reply via email to