1

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)
Community
  • 1
  • 1
Petar
  • 2,241
  • 1
  • 24
  • 38

3 Answers3

0

The problem is that android doesn't have xerces and validating interface =) And BTW xerces ASIS from box don't work in android, because some classes are excluded from final APK because they compiled with different target. But you can simply add validating from xerce:

You can use xerces and native schema validation (in java) in adnroid - you have to download xerces sources and (after some simple manipulations) include it to your own code - you will be able to use DocumentBuilderFactory.setShema method.

https://stackoverflow.com/questions/13142567/xml-schema-validation-xmlsignature-with-xerces-in-android

Community
  • 1
  • 1
dilix
  • 3,761
  • 3
  • 31
  • 55
0

It's a knowed issue posted by google at http://code.google.com/p/android/issues/detail?id=7395

The solution is use Apache Xerces ported to Android like you say. There is a proyect here http://code.google.com/p/xerces-for-android/

You have to do a svn chekout and export the proyect to a jar file to use as a library in your android proyect.

The code to instance SchemaFactory change a little. I show you an example:

import mf.javax.xml.validation.Schema;
import mf.javax.xml.validation.SchemaFactory;
import mf.javax.xml.validation.Validator;
import mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory;


SchemaFactory  factory = new XMLSchemaFactory();
Schema esquema = factory.newSchema(".../file.xsd");
el_mapleee
  • 91
  • 3
0

I found I was not able to get the regular xerces to work with Android, however I did find Xerces-for-Android, which I got working. The following are details of the setup and some example code. Good luck :)

The following worked for me:

  1. Create a validation utility.
  2. Get both the xml and xsd into file on the android OS and use the validation utility against it.
  3. Use Xerces-For-Android to do the validation.

Android does support some packages which we can use, I created my xml validation utility based on: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html

My initial sandbox testing was pretty smooth with java, then I tried to port it over to Dalvik and found that my code did not work. Some things just aren't supported the same with Dalvik, so I made some modifications.

I found a reference to xerces for android, so I modified my sandbox test of (the following doesn't work with android, the example after this does):

import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.Document;

/**
 * A Utility to help with xml communication validation.
 */
public class XmlUtil {

    /**
     * Validation method. 
     * Base code/example from: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html
     * 
     * @param xmlFilePath The xml file we are trying to validate.
     * @param xmlSchemaFilePath The schema file we are using for the validation. This method assumes the schema file is valid.
     * @return True if valid, false if not valid or bad parse. 
     */
    public static boolean validate(String xmlFilePath, String xmlSchemaFilePath) {

        // parse an XML document into a DOM tree
        DocumentBuilder parser = null;
        Document document;

        // Try the validation, we assume that if there are any issues with the validation
        // process that the input is invalid.
        try {
            // validate the DOM tree
            parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            document = parser.parse(new File(xmlFilePath));

            // create a SchemaFactory capable of understanding WXS schemas
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            // load a WXS schema, represented by a Schema instance
            Source schemaFile = new StreamSource(new File(xmlSchemaFilePath));
            Schema schema = factory.newSchema(schemaFile);

            // create a Validator instance, which can be used to validate an instance document
            Validator validator = schema.newValidator();
            validator.validate(new DOMSource(document));
        } catch (Exception e) {
            // Catches: SAXException, ParserConfigurationException, and IOException.
            return false;
        }     

        return true;
    }
}

The above code had to be modified some to work with xerces for android (http://gc.codehum.com/p/xerces-for-android/). You need SVN to get the project, the following are some crib notes:

download xerces-for-android
    download silk svn (for windows users) from http://www.sliksvn.com/en/download
        install silk svn (I did complete install)
        Once the install is complete, you should have svn in your system path.
        Test by typing "svn" from the command line.
        I went to my desktop then downloaded the xerces project by:
            svn checkout http://xerces-for-android.googlecode.com/svn/trunk/ xerces-for-android-read-only
        You should then have a new folder on your desktop called xerces-for-android-read-only

With the above jar (Eventually I'll make it into a jar, just copied it directly into my source for quick testing. If you wish to do the same, you can making the jar quickly with Ant (http://ant.apache.org/manual/using.html)), I was able to get the following to work for my xml validation:

import java.io.File;
import java.io.IOException;

import mf.javax.xml.transform.Source;
import mf.javax.xml.transform.stream.StreamSource;
import mf.javax.xml.validation.Schema;
import mf.javax.xml.validation.SchemaFactory;
import mf.javax.xml.validation.Validator;
import mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory;

import org.xml.sax.SAXException;

/**
 * A Utility to help with xml communication validation.
 */public class XmlUtil {

    /**
     * Validation method. 
     * 
     * @param xmlFilePath The xml file we are trying to validate.
     * @param xmlSchemaFilePath The schema file we are using for the validation. This method assumes the schema file is valid.
     * @return True if valid, false if not valid or bad parse or exception/error during parse. 
     */
    public static boolean validate(String xmlFilePath, String xmlSchemaFilePath) {

        // Try the validation, we assume that if there are any issues with the validation
        // process that the input is invalid.
        try {
            SchemaFactory  factory = new XMLSchemaFactory();
            Source schemaFile = new StreamSource(new File(xmlSchemaFilePath));
            Source xmlSource = new StreamSource(new File(xmlFilePath));
            Schema schema = factory.newSchema(schemaFile);
            Validator validator = schema.newValidator();
            validator.validate(xmlSource);
        } catch (SAXException e) {
            return false;
        } catch (IOException e) {
            return false;
        } catch (Exception e) {
            // Catches everything beyond: SAXException, and IOException.
            e.printStackTrace();
            return false;
        } catch (Error e) {
            // Needed this for debugging when I was having issues with my 1st set of code.
            e.printStackTrace();
            return false;
        }

        return true;
    }
}

Some Side Notes:

For creating the files, I made a simple file utility to write string to files:

public static void createFileFromString(String fileText, String fileName) {
    try {
        File file = new File(fileName);
        BufferedWriter output = new BufferedWriter(new FileWriter(file));
        output.write(fileText);
        output.close();
    } catch ( IOException e ) {
       e.printStackTrace();
    }
}

I also needed to write to an area that I had access to, so I made use of:

String path = this.getActivity().getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir;   

A little hackish, it works. I'm sure there is a more succinct way of doing this, however I figured I'd share my success, as there weren't any good examples that I found.

James Oravec
  • 19,579
  • 27
  • 94
  • 160