1

I am trying xml validation against given xsd file in android and everytime I include the following line the application stops unexpectedly:

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

Here is relevant code snippet:

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

   File schemaLocation = new File("/sdcard/sep.xsd");
   Schema schema = factory.newSchema(schemaLocation);

   Validator validator = schema.newValidator();

   Source source = new StreamSource("/sdcard/gtp.xml");

    try {          
        validator.validate(source);
    }
    catch (SAXException ex) {
    Toast toast = Toast.makeText(this, "invalid", Toast.LENGTH_SHORT);
    toast.show();
        }  
leppie
  • 115,091
  • 17
  • 196
  • 297
user1841900
  • 51
  • 1
  • 3

1 Answers1

0

This is the solution:

File xmlData;
File xmlSchema;
xmlData = new File( xmlDataName );
xmlData = new File( xmlSchemaName );
SchemaFactory schemafactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource( xmlSchema );
Source xmlSource = new StreamSource( xmlData );
Schema schema = schemafactory.newSchema( schemaFile );
Validator validator = schema.newValidator();
validator.validate( xmlSource );

But you need to add a XML Validation library to your project.

For example: https://code.google.com/p/xerces-for-android/.

Erdinc Ay
  • 3,224
  • 4
  • 27
  • 42