3

I have written a code to validate given xml file against given xsd file in my android application. But it is giving Illeagal exception error. In some posts I saw its because of low java version. But my java version 1.6.0_20. please check the below code, Error log and suggest.

Code:

    try {
        // parse an XML document into a DOM tree
        parserFactory = DocumentBuilderFactory.newInstance();
        parserFactory.setNamespaceAware(true);
    } catch (Exception e) {
        Log.e("Exception", "ERROR Last : " + e);
        e.printStackTrace();
    }

    DocumentBuilder parser = null;
    try {
        parser = parserFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e1) {
        e1.printStackTrace();
        Log.e("Exception", "ERROR 1: " + e1);
    }
    Document document = null;
    try {
        document = parser.parse(getResources().openRawResource(
                R.raw.change_pin_request_xaml));
    } catch (SAXException e1) {
        e1.printStackTrace();
        Log.e("Exception", "ERROR 2 : " + e1);

    } catch (IOException e1) {
        e1.printStackTrace();
        Log.e("Exception", "ERROR 3 : " + e1);
    }

    try {
        // create a SchemaFactory capable of understanding WXS schemas
        factory = SchemaFactory
                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // W3C_XML_SCHEMA_INSTANCE_NS_URI 
        // W3C_XML_SCHEMA_NS_URI

        // load a WXS schema, represented by a Schema instance
        schemaFile = new StreamSource(getResources().openRawResource(
                R.raw.change_pin_request_xsd));
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("Exception", "ERROR special " + e);
    }
    Schema schema = null;

    try {
        schema = factory.newSchema(schemaFile);
    } catch (SAXException e1) {
        Log.e("Exception", "ERROR 4 : " + e1);
        e1.printStackTrace();
    }

    // create a Validator instance, which can be used to validate an
    // instance document
    Validator validator = schema.newValidator();

    // validate the DOM tree
    try {
        try {
            validator.validate(new DOMSource(document));
        } catch (IOException e) {
            Log.e("Exception", "ERROR 5 : " + e);
            e.printStackTrace();
        }
    } catch (SAXException e) {
        Log.e("Exception", "ERROR 6 : " + e);

    }

Error:

04-30 20:34:12.658: W/System.err(921): java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
04-30 20:34:12.658: W/System.err(921):  at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:192)
04-30 20:34:12.658: W/System.err(921):  at com.xml.xsd.test.XmlXsdTestActivity.onCreate(XmlXsdTestActivity.java:64)
04-30 20:34:12.658: W/System.err(921):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-30 20:34:12.658: W/System.err(921):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-30 20:34:12.658: W/System.err(921):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-30 20:34:12.658: W/System.err(921):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-30 20:34:12.658: W/System.err(921):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-30 20:34:12.658: W/System.err(921):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 20:34:12.658: W/System.err(921):  at android.os.Looper.loop(Looper.java:123)
04-30 20:34:12.658: W/System.err(921):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-30 20:34:12.669: W/System.err(921):  at java.lang.reflect.Method.invokeNative(Native Method)
04-30 20:34:12.669: W/System.err(921):  at java.lang.reflect.Method.invoke(Method.java:507)
04-30 20:34:12.669: W/System.err(921):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-30 20:34:12.669: W/System.err(921):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-30 20:34:12.669: W/System.err(921):  at dalvik.system.NativeStart.main(Native Method)
04-30 20:34:12.669: E/Exception(921): ERROR special java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
candy
  • 734
  • 1
  • 11
  • 22
  • Possible duplicate of http://stackoverflow.com/questions/3129934/schemafactory-doesnt-support-w3c-xml-schema-in-platform-level-8 – THelper May 01 '12 at 08:23
  • After trying all those ways...I posted the question here... – candy May 01 '12 at 08:49

1 Answers1

0

This should be a comment but since I can't make comments ill make it an answer.

Right click on your project in Package Explorer > Properties > Java Compiler

and see if its compiling using 1.6. I had a weird problem and it was because eclipse was compiling in 1.5.

user1378730
  • 930
  • 9
  • 18
  • My eclipse is using Java compiler 1.6 only. I checked it already, cause I also had this type of issues in my old projects, anyway thanks for the info. – candy May 07 '12 at 05:20