Hello Madhav
Thanx for Your advice so far
Yes I have had som misunderstanding about what the parser should be
"feeded" with. At first it was feeded with the XML file where the parser
sayes:
File <The whole xml notatation> not found. But my XML is unfortunately not
showed in the WSAD-debugger console. So my message from the debugger was
File not found
I changed to feed the parser like this
String document ="";
document = "resource/ModtagKvitteringer/xerces_IN_1.xml";
parser.parse(document);
And the console output goes like this:
resource/ModtagKvitteringer/xerces_IN_1.xml is valid.
But it seems to validate for well-formnedness and IO only and not
for elements definitions in the XSD files. (I change the element values
to invalid values according to the XSD's)
I have run through Roseannes FAQ on XML, quite comprehensive I might say
So the next excersice will be to reach the external XSD :
"http://editest.forsikringenshus.dk/ATP/XMLdef/ATPSPkonti.xsd
The only thing that is given right now, is when I let WSAD's XML validator
validate the XML files
I have placed the Class below cleaned for corporate packages
(Developed on SDK 1.4)
per aspera ad astra
/*
* Created on 26-11-2004
*
* To change the template for this generated file go to
* Window>Preferences>
Java>Code Generation>Code and Comments
*/
package dk.atp.sp.fritvalg.broker;
import java.io.*;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.IOException;
/**
* @author tsn
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class ATPXercesChecker implements ErrorHandler {
// Flag to check whether any errors have been spotted.
private boolean valid = true;
public boolean isValid() {
return valid;
}
// If this handler is used to parse more than one document,
// its initial state needs to be reset between parses.
public void reset() {
// Assume document is valid until proven otherwise
valid = true;
}
public void warning(SAXParseException exception) {
System.out.println("Warning: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber() + ", column " + exception.getColumnNumber());
System.out.println(" in entity " + exception.getSystemId());
}
public void error(SAXParseException exception) {
System.out.println("Error: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber() + ", column " + exception.getColumnNumber());
// Unfortunately there's no good way to distinguish between
// validity errors and other kinds of non-fatal errors
valid = false;
}
public void fatalError(SAXParseException exception) {
System.out.println("Fatal Error: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber() + ", column " + exception.getColumnNumber());
System.out.println(" in entity " + exception.getSystemId());
}
String getMessage(String level, SAXParseException e) {
return ( "Parsing " + level + "\n" + "Line: " + e.getLineNumber() + "\n" + "URI: " + e.getSystemId() + "\n" + "Message: " + e.getMessage());
}
public static void main(String[] args) {
String document ="";
try {
document = "resource/ModtagKvitteringer/xerces_IN_1.xml";
XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
ATPXercesChecker handler = new ATPXercesChecker();
parser.setErrorHandler(handler);
// This is a hack to fit some long lines of code that
// follow between the margins of this printed page
String features = "http://apache.org/xml/features/";
// Turn on Xerces specific features
parser.setFeature(features + "validation/dynamic", true);
parser.setFeature(features + "validation/schema-full-checking", true);
parser.setFeature(features + "validation/warn-on-duplicate-attdef", true);
parser.setFeature(features + "validation/warn-on-undeclared-elemdef", true);
parser.setFeature(features + "continue-after-fatal-error", true);
//Turn on other features
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://editest.forsikringenshus.dk/ATP/XMLdef/ATPSPkonti.xsd");
parser.parse(document);
if (handler.isValid()) {
System.out.println(document + " is valid.");
}
else {
// If the document isn't well-formed, an exception has
// already been thrown and this has been skipped.
System.out.println(document + " is well-formed.");
}
}
catch (SAXParseException e) {
System.out.print(document + " is not well-formed at ");
System.out.println("Line " + e.getLineNumber() + ", column " + e.getColumnNumber()+ " in file " + e.getSystemId());
}
catch (SAXException e) {
System.out.println("Could not check document because " + e.getMessage());
}
catch (IOException e) {
System.out.println(
"Due to an IOException, the parser could not check " + document);
}
catch (Exception e) {
System.out.println("Due to an Exception, the parser could not check " + document);
}
}
}