3

我已经编写了一个代码来验证给定的 xml 文件与我的 android 应用程序中给定的 xsd 文件。但它给出了非法异常错误。在一些帖子中,我看到它是因为 Java 版本低。但是我的 java 版本是 1.6.0_20。请检查以下代码、错误日志和建议。

代码:

    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);

    }

错误:

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
4

1 回答 1

0

这应该是一条评论,但由于我无法发表评论,因此无法将其作为答案。

在 Package Explorer > Properties > Java Compiler 中右键单击您的项目

看看它是否使用 1.6 编译。我遇到了一个奇怪的问题,这是因为 eclipse 是在 1.5 中编译的。

于 2012-05-07T04:58:42.637 回答