So, after consulting with this thread that the javax.xml.validation library does not work on Android I had to find another solution. I have tried using the Xerces API and although it seems that it is working fine for a lot of people, I can't get it to work properly.
I am using a local XML schema stored in a file in the sdcard.
The code I am using is as follows:
public static boolean validate(String XmlDocumentUrl, String SchemaUrl) {
SAXParser parser = new SAXParser();
try {
parser.setFeature("http://xml.org/sax/features/namespaces", true);
parser.setFeature("http://xml.org/sax/features/namespace-prefixes", 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/standard-uri-conformant", false);
parser.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation",
"http://www.topografix.com/GPX/1/0 file:///mnt/sdcard/gpxSchema1.0.xsd");
Validator handler = new Validator();
parser.setErrorHandler(handler);
parser.parse(XmlDocumentUrl);
if (handler.validationError == true){
System.out.println("XML Document has Error:"
+ handler.validationError + ""
+ handler.saxParseException.getMessage());
return false;
}
else{
System.out.println("XML Document is valid");
return true;
}
} catch (java.io.IOException ioe) {
System.out.println("IOException" + ioe.getMessage());
} catch (SAXException e) {
System.out.println("SAXException" + e.getMessage());
}
return false;
}
private static class Validator extends DefaultHandler {
public boolean validationError = false;
public SAXParseException saxParseException = null;
public void error(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException = exception;
}
public void fatalError(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException = exception;
}
public void warning(SAXParseException exception) throws SAXException {
}
}
By experimenting with this code I am thinking that this is causing the problem:
parser.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation",
"http://www.topografix.com/GPX/1/0 file:///mnt/sdcard/gpxSchema1.0.xsd");
I am thinking that for some reason the .xsd file is not found but I am not sure about it. I would be happy if anyone could explain we what I am doing wrong, or if there is something unrelated with this property and is still not right.
The error I get is as follows:
04-03 18:12:05.125: E/AndroidRuntime(20457): FATAL EXCEPTION: main
04-03 18:12:05.125: E/AndroidRuntime(20457): java.lang.***ExceptionInInitializerError***
04-03 18:12:05.125: E/AndroidRuntime(20457): at org.apache.xerces.parsers.XML11Configuration.configurePipeline(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457): at org.apache.xerces.parsers.XIncludeAwareParserConfiguration.configurePipeline(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457): at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457): at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457): at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457): at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.inputhandling.util.XMLSchemaValidator2.validate(XMLSchemaValidator2.java:29)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.inputhandling.validation.gpx.schemas.StrategyGPXSchema1_0.validate(StrategyGPXSchema1_0.java:11)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.inputhandling.validation.gpx.schemas.GPXSchemaValidatorGeneral.executeStrategy(GPXSchemaValidatorGeneral.java:13)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.inputhandling.validation.gpx.GPXValidator.isValidAgainstSchema(GPXValidator.java:115)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.inputhandling.validation.gpx.GPXValidator.validate(GPXValidator.java:34)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.inputhandling.validation.ValidatorGeneral.executeStrategy(ValidatorGeneral.java:15)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.inputhandling.MecheTrackParser.isValidMecheFile(MecheTrackParser.java:55)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.inputhandling.MecheTrackParser.parse(MecheTrackParser.java:30)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.statePattern.states.InitState.selectFile(InitState.java:33)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.main.MecheModel.selectFile(MecheModel.java:39)
04-03 18:12:05.125: E/AndroidRuntime(20457): at com.pe60t0.Meche.MecheActivity$1.onClick(MecheActivity.java:118)