Hi mukul and Michael,
sorry for this communication gap i was on vacation.well thanks for your time
and reply...but problem is; In our system we need to capture all th well formed
errors in one go. getting one fatal error is not acceptable as per our
requirement.
it will be a great help if you can let me know where i am doing wrong for this
(need urgent solution) or atleast let me know where can i find solution.
The (relevant portion of the) XML document follows:(with two well formed
errors)***************
<?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:string" />
<xs:element name="schemaFile" type="xs:string" />
<xs:element name="validationHierarchy" type="xs:string" />
<xs:element name="database" type="xs:string" />
</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();
}
}
}
I have put xml-apis.jar, xercesImpl.jar, serializer.jar, resolver.jar at build
path.
Unfortunately, when I try to validate the above XML document against the
Schema, it throws errors as I explained above
only one fatal error.
Errors that I got*****************************
Exception at:8***The element type "validationHierarchy" must be terminated by
the matching end-tag "</validationHierarchy>".
Exception at:8***The end-tag for element type "validationHierarchy" must end
with a '>' delimiter.
Exception at:9***XML document structures must start and end within the same
entity.
Exception at:-1***Premature end of file.
*************************
it will be a great help if you can let me know where i am doing wrong for this
(need urgent solution) or atleast let me know where can i find solution.
Thanks
Vineet Rajput
Software Engineer
Snap-On Business Solution
Logix Techno Park,
Noida,INDIA