SchemaFactory不支持平台级别8中的W3C XML Schema?

31 xml android xsd android-2.2-froyo

使用Android SDK,普通空活动中的以下代码将失败:

@Override
protected void onStart() {
    super.onStart();

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
}
Run Code Online (Sandbox Code Playgroud)

2.2模拟器logcat显示此异常:

06-28 05:38:06.107: WARN/dalvikvm(495): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-28 05:38:06.128: ERROR/AndroidRuntime(495): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.HelloWorldActivity}: java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
        at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:194)
        at com.example.HelloWorldActivity.onStart(HelloWorldActivity.java:26)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
        at android.app.Activity.performStart(Activity.java:3781)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
        ... 11 more
Run Code Online (Sandbox Code Playgroud)

SchemaFactoryJavadoc提到"平台默认SchemaFactory位于特定于实现的方式.必须有一个平台默认的SchemaFactory用于W3C XML Schema."

Avi*_*rry 1

您可能会运气好,用 jarjar 重新打包 xerces,然后通过

"org.apache.xerces.jaxp.validation.XMLSchemaFactory"
Run Code Online (Sandbox Code Playgroud)

SchemaFactory.newInstance(String schemaLanguage, String factoryClassName, ClassLoader classLoader)
Run Code Online (Sandbox Code Playgroud)

如果您使用 API >=9 或直接实例化

org.apache.xerces.jaxp.validation.XMLSchemaFactory 
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 API 8。使用比该版本更旧的 API 可能根本无法工作。