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.
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.
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.
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.
According to the documentation javax.xml.validation is supported since API level 8.
(I will test that and report asap)
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).
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");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With