I am trying to validate a XML file against XSD on Android devices.
I googled a lot and found some solution like xerces-for-android.
In the Stack overflow I found some page like this, which suggests to avoid javax.xml
. validation and use Xerces for this purpose.
I tested it in the different Android APIs(17, 20,25)
but unfortunately I did not have any success.
Could you please help me and suggest an alternative method.
1 - Go to https://code.google.com/archive/p/xerces-for-android/
2 - Click on source in left menu & in source in the middle page.
3 - Click on download the code & extract the zip contents.
4 - In the extracted content navigate to the folder "xerces-for-android/trunk/src".
5 - Copy the folder "mf" in your [AndroidApp|AndroidModule] "src/main/java" directory.
6 - Now you have to create a folder called "resources" on your "src/main" directory. This can be done in Android Studio (RightClick on your Module/App -> New -> Folder -> Java Resource Folder).
7 - Inside that folder you must create the next folder structure:
mf/org/apache/xerces/impl/msg
&
mf/org/apache/xerces/impl/xpath/regex
8 - Copy the files with ".properties" extension from the equivalent extranted contents folders inside this directories.
When you validate the XML files with XSD with something like that
SchemaFactory factory = new XMLSchemaFactory();
Source schemaFile = new StreamSource( /*Input stream to your schema*/ );
Source xmlSource = new StreamSource( /*Input stream to your xml source*/ );
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlSource);
Don't use the SDK imports and use the mf... package ones.
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;
Edited to reply the comment.
When I was looking for information on how to validate Xml documents with Xml Schema in Android I stumbled upon comments about this project, although it only contained a zip and no documentation.
I looked for a repository that had commits and found this https://github.com/MaTriXy/xerces-for-android where the last commit was made 4 years ago.
After copying the .java files but not the .profile within resources, an exception was thrown, saying that the .profile were not found, look for new information and I saw that the .profile have to be inside the resources folder in Android Studio.
Probably, and this is an assumption, the project was a jar for Eclipse/Ant and now does not work with AndroidStudio/Gradle.
You can create an .aar. Create an Android library module in your project, perform the same steps and compile the application for release.
Within your project, in the directory.
/Yourproject/yourmodule/build/outputs/aar/
There will be a file called yourmodule-release.arr and you should be able to use that package and delete the module.
Looking for information I also found information about jarjar which seems to be a project to make this adaptation in a simpler way.
https://github.com/shevek/jarjar
Have a nice day.