2012-12-04 42 views
2

我正在開發一個Android應用程序,需要根據XML字符串動態呈現組件。這個XML字符串基於XSD,我直接從服務器獲取XML。我已經使用JAXB xjc從XSD創建對象模型。但是現在我希望能夠用JAXB解組和解析完整的XML。 我在這裏和this post經歷了幾個職位,我找到了一個解決方案,最後的解決方案給出solution使用重新包裝的jaxb庫。我跟着下面的步驟: -Android上重新打包的JAXB以運行時結束異常

1. 導入圖書館的: 的activation.jar additionnal.jar AE-JAXB-2.2.5.jar iStack技術,公地runtime.jar JAXP的datatype.jar txw2-20110809.jar

  1. 將我的jaxb導入替換爲ae.jaxb。

  2. 添加了​​ 到我的代碼中的onCreate方法。這裏RenderXml是我的Activity的名字。

我仍然得到以下運行時錯誤: -

12-04 13:44:39.820: E/AndroidRuntime(2219): FATAL EXCEPTION: main 
12-04 13:44:39.820: E/AndroidRuntime(2219): java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlRegistry 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at java.lang.Class.getDeclaredAnnotation(Native Method) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at java.lang.Class.getAnnotation(Class.java:260) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.com.sun.xml.bind.v2.model.annotation.RuntimeInlineAnnotationReader.getClassAnnotation(RuntimeInlineAnnotationReader.java:109) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.com.sun.xml.bind.v2.model.annotation.RuntimeInlineAnnotationReader.getClassAnnotation(RuntimeInlineAnnotationReader.java:59) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:330) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:461) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:299) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:142) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1164) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:146) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:237) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:186) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:146) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.javax.xml.bind.ContextFinder.find(ContextFinder.java:361) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:446) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:409) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at ae.javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:313) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at com.example.testuirenderxml.RenderXml.generateFirstMenu(RenderXml.java:79) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at com.example.testuirenderxml.RenderXml.onCreate(RenderXml.java:112) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at android.app.Activity.performCreate(Activity.java:5104) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at android.os.Looper.loop(Looper.java:137) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at dalvik.system.NativeStart.main(Native Method) 
12-04 13:44:39.820: E/AndroidRuntime(2219): Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.xml.bind.annotation.XmlRegistry" on path: /data/app/com.example.testuirenderxml-2.apk 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
12-04 13:44:39.820: E/AndroidRuntime(2219):  ... 35 more 

我有需要使用JAXB,因爲我有機會從定期更新的XSD對象模型,有沒有什麼其他方式來創建對象模型,然後藉助這個對象模型來解析xml以實現我想要的目標?

+0

我找到了這個答案,但現在我得到一個「沒有XMLSchema註釋找到」與JAXB xjc生成的類...我需要使用重新打包的JAXB重新生成對象模型? – Ricky

回答

0

我找到了我的問題的答案。我需要在我的onCreate方法中設置以下內容: -

RuntimeInlineAnnotationReader.cachePackageAnnotation(LayoutT.class.getPackage(), new XmlSchemaMine("myNameSpace")); 

其中,myNameSpace是我的XSD架構的名稱空間。

+0

LayoutT是我的對象模型中使用JAXB xjc生成的類中的一個類...第一個參數基本上是對象模型的包名稱。 – Ricky