Hi out there, I have written an XMLValidator class, which validates an XML String against an given Schema (-file). The XML String does not contain any hint on the schema. I attached the source code.
Running under Linux (Java 5, Xerces 2.6.2) the result is true, vereything is fine. If I port to Windows (same java, same xerces version) the result with String fileName = "H:/source/testschema.xsd"; I get the following error: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'address-book'. false Whats wrong? Any suggestions? Thanks alot! Kind regards Joern
/*
* Main.java
*
* Created on 29. April 2005, 22:18
*/
package javatestapplications;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.*;
/**
*
* @author [EMAIL PROTECTED]
*/
public abstract class XMLValidator {
/** Creates a new instance of Main */
public XMLValidator() {
}
public static boolean parse(String xmlDocument, java.net.URL schemaURL) {
// Create a Xerces SAX Parser
SAXParser parser = new SAXParser();
try {
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.setErrorHandler(new ErrorsAreFatal());
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",schemaURL.toString());
java.io.InputStream is = new java.io.ByteArrayInputStream(xmlDocument.getBytes());
org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(is);
parser.parse(inputSource);
return true;
} catch (Exception ex) {
System.err.println(ex.toString());
return false;
}
}
public static void main(String[] args) {
String xmlString = "<address-book>" +
"<item><name>Joern</name><email>email</email><mobile>049-12346</mobile></item>" +
"</address-book>";
String fileName = "/home/jomu/src/JavaTestApplications/src/javatestapplications/testschema.xsd";
try {
// java.net.URL schemaURL = XMLValidator.class.getClassLoader().getResource(fileName);
java.net.URL schemaURL = new java.net.URL("file://"+fileName);
System.out.println(XMLValidator.parse(xmlString, schemaURL));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
