I have a project with API 10 (2.3.3 version) in Android and i have a problem for the validation of xml with xsd file.
This is my code:
public static Document buildDoc(String xml, String xsd){
// parse an XML document into a DOM tree
Document document = null;
try {
DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
parserFactory.setNamespaceAware(true);
DocumentBuilder parserdb = parserFactory.newDocumentBuilder();
doc = parserdb.parse(new InputSource( new StringReader(xml) ));
SchemaFactory factory = SchemaFactory.newInstance(
XMLConstants.W3C_XML_SCHEMA_NS_URI); //here the emulator raises an exception
Source schemaFile = new StreamSource(new File(xsd));
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(doc));
System.out.println("Validation OK!");
} catch (SAXException e) {
// instance document is invalid!
System.err.println("Validation ERROR!");
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
System.err.println("Validation ERROR!");
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
System.err.println("Validation ERROR!");
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Validation ERROR!");
e.printStackTrace();
}
return doc;
}
My Eclipse Simulator throw an exception:
E/AndroidRuntime(4770): Caused by: java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
In this line:
SchemaFactory factory = SchemaFactory.newInstance(
XMLConstants.W3C_XML_SCHEMA_NS_URI);
Why??
Because XML Schema isn’t supported on your platform.