Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android schema validation

Tags:

java

android

xsd

I have created an XML and i am having an XSD file and i have to validate the xml with this schema ,can i get any example for doing that . And where i have to place the xsd file in my project so that i can validate with that schema.

like image 450
Sam97305421562 Avatar asked Apr 29 '09 06:04

Sam97305421562


People also ask

What is meant by schema validation?

An API schema defines which API requests are valid based on several request properties like target endpoint and HTTP method. Schema Validation allows you to check if incoming traffic complies with a previously supplied API schema.

What is xmlns Android?

Xmlns means xml namespace. It is created to avoid naming conflicts in the xml's. In order to avoid naming conflicts by any other way we need to provide each element with a prefix. to avoid repetitive usage of the prefix in each xml tag we use xmlns at the root of the xml.

What is schema in Kotlin?

Schemas play a central role in the GraphQL world. Traditionally, GraphQL services are developed following a schema-first approach, where you first manually define your schema using Schema Definition Language (SDL) and then map it to corresponding resolvers and types.


3 Answers

According to the documentation javax.xml.validation is supported since API level 8.

(I will test that and report asap)

Update

Ok, the problem is not that simple:

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

failed on both level 8 and level 9 API with an IllegalArgumentException as described here

Google doesn't help on that matter except to find the very same failure report. The API is here but not the implementation (as default).

like image 85
Renaud Avatar answered Sep 26 '22 22:09

Renaud


It's a known issue posted by Google here

The solution is to use Apache Xerces ported to Android. There is a project here

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");
like image 42
el_mapleee Avatar answered Sep 26 '22 22:09

el_mapleee


This may not help a lot, but last I checked, Android platform was somewhat limited Java environment. And the main problem is that some APIs one would expect to be available -- specifically, javax.xml.validation (part of JDK 1.5 and anove!) -- are not there.

As such you may need to include more jars than would be done on a non-castrated java platform. Further there may be limitations as to what standard APIs can be added, due to black/whitelisting problems (this is a big problem with Google AppEngine, and since Android predates it, it has similar challenges).

That aside, I would try to use javax.xml.validation with bundled XML parser Xerces. There are gazillions of how-to documents on how to do that.

like image 36
StaxMan Avatar answered Sep 24 '22 22:09

StaxMan