2

我正在开发一个需要根据 XML 字符串动态呈现组件的 Android 应用程序。此 XML 字符串基于 XSD,我直接从服务器获取 XML。我使用 JAXB xjc 从 XSD 创建对象模型。但现在我希望能够使用 JAXB 解组和解析完整的 XML。我在这里浏览了几篇文章,在这篇文章中,我找到了一个解决方案,其中最后一个解决方案提供了使用重新打包的 jaxb 库的解决方案。我按照以下步骤操作:-

1.导入库:activation.jar additionalnal.jar ae-jaxb-2.2.5.jar istack-commons-runtime.jar jaxp-datatype.jar txw2-20110809.jar

  1. 将我的 jaxb 导入替换为 ae.jaxb。

  2. 添加RuntimeInlineAnnotationReader.cachePackageAnnotation(RenderXml.class.getPackage(), new XmlSchemaMine("")); 到我的代码中的 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 以实现什么我想要?

4

1 回答 1

0

我找到了我的问题的答案。我需要在我的 onCreate 方法中设置以下内容:-

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

其中 myNameSpace 是我的 XSD 架构的命名空间。

于 2012-12-07T08:28:56.313 回答