• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to Validate XML file in Android using XSD Schema ..?

try {
DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
parserFactory.setNamespaceAware(true);
DocumentBuilder parser = parserFactory.newDocumentBuilder();
Document document = parser.parse(new File(SDCardManager.getSDCardManagerInstance().getBaseDir() + "/"
+ xmlName));

// 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(SDCardManager.getSDCardManagerInstance().getBaseDir()
+ "/file.xsd"));
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();

// validate the DOM tree

validator.validate(new DOMSource(document));
return true;
} catch (Exception e) {
Log.e("XML Exception", e.getMessage());
return false;
}

i am trying this code to validate the XML using XSD schema but i am facing an illegalArgumentException on
(SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)).Kindly help me out that how to resolve it....
 
Hi!

This is a knowed issue posted by google at "code.google.com/p/android/issues/detail?id=7395"

The solution is use Apache Xerces ported to Android.
There is a proyect with this here "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");
 
Although you cannot validate XML files against an XML Schema you can check the XML syntax. I find that the NetBeans IDE is terrific for both XML Checking and validation. Here is how...

* Open file in NetBeans IDE using File => Open File
* Use right-click in editor window and choose "Check XML"
* Fix any errors reported and repeat "Check XML" until no more errors are reported
 
Back
Top Bottom