【问题标题】:Using JAXB with Google Android将 JAXB 与 Google Android 一起使用
【发布时间】:2011-07-24 14:02:36
【问题描述】:

我需要解析通过 xml 传递的 java 对象。 我想将 JAXB 框架用于这些目的,因为我已经预先注释(使用 JAXB)java 类。

这原则上可行吗?

InputStream input = entity.getContent();
JAXBContext jc = JAXBContext.newInstance(new Class[] {Response.LoginResponse.class});
Unmarshaller un = jc.createUnmarshaller();
LoginResponse response = (LoginResponse)un.unmarshal(input);

在第 4 行,我有一个警告:“无法解析静态方法 282 JAXBContext ...” 然后虚拟机崩溃

关于如何解决这个问题的任何想法?

【问题讨论】:

  • 我重新打包了 JAXB。详情请见this blog

标签: android jaxb


【解决方案1】:

我并没有完全解决您的问题,但默认情况下,JAXB 不包含在 android 中,并且该库将花费您 9(!) MB 的 apk。请改用SimpleXML。它具有类似的能力,而且更轻量级。

【讨论】:

  • 我想指出,我已经预先注释(使用 JAXB)java 类。如果我将使用 SimpleXML,我需要重新注释类。这在我的项目中是不可接受的。
  • 但是 SimpleXML 并不完全支持 XML Schema。
  • SimpleXML 存在很多问题,我无法证明这种权衡是合理的,例如强制插入某些属性(这对某些模式无效)。如果我的生产者/消费者都是 SimpleXML,那很好,但不是。
  • Kevin Mangold,注释中有一些属性,例如“严格”之一,可以让您忽略缺失的属性。
  • 不回答问题 - 问题是如何使用 PRE-ANNOTATED JAXB 类。
【解决方案2】:

我有同样的问题,想在解析 XML(对我来说是 JSON)Web 服务器响应的过程中使用一堆 JAXB 类。

我为 Parsing XML and JSON on Android 编写了一个 wiki 页面,其中描述了此问题的解决方案并指导您逐步完成。

明确一点 - 我建议尽可能在 Android 上使用 JSON 而不是 XML。有significant performance advantages for JSON

总结一下我使用 JAXB 类进行 JSON 和 XML 解析的解决方案:

我最终从 JAXB 类中剥离了所有注解和 javax.xml.* 导入(比为 SimpleXML 重新注解类稍微容易一些 - 另外,您还可以解析 JSON 响应)以创建真正的 POJO,然后将 Jackson 与Jackson 所依赖的 XML 库的修改版本。

XML 库的修改是必要的,因为 Android 在平台中不包含所需的 XML 类,如果您尝试将受保护的 javax.xml 命名空间中的类作为库包含在项目中,它将complain

我最初手动剥离了 XML 注释和导入,但最终得到了automating the process via ant scripts

幸运的是,您可以自动化 XML 库修改过程。我在Modifying XML libraries for Android(完整教程引用)上写了一篇详细的帖子,其中描述了如何使用Jar Jar Links utility 将相关类移动到新的不受保护的命名空间中。

如果您想在 Android 上使用 Jackson,我还为 Android 创建了 jackson-dataformat-androidaalto-xmlstax-apistax2-api 项目的修改版本,您可以随意使用。

这是一个示例,您将在 pom.xml 中包含通过 Maven 包含依赖项的内容:

<dependencies>              
    <!-- Using Jackson for parsing -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.1.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.1.2</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.1.2</version>
    </dependency>   
    <!-- XML parsing  -->
    <!-- So many problems with XML and Android...
         Below XML libraries have been modified using JarJar
         and are NOT the same as their J2SE counterparts,
         hence the added "-android" to the artifactId
         See:
         https://github.com/CUTR-at-USF/SiriRestClient/wiki/Modifying-XML-libraries-for-Android
      -->        
    <dependency>
        <groupId>edu.usf.cutr.android.xml</groupId>
        <artifactId>jackson-dataformat-xml-android</artifactId>
        <version>2.1.2</version>
    </dependency>
    <dependency>
        <groupId>edu.usf.cutr.android.xml</groupId>
        <artifactId>stax2-api-android</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>edu.usf.cutr.android.xml</groupId>
        <artifactId>stax-api-android</artifactId>
        <version>1.0-2</version>
    </dependency>
    <dependency>
        <groupId>edu.usf.cutr.android.xml</groupId>
        <artifactId>aalto-xml-android</artifactId>
        <version>0.9.8</version>
    </dependency>        
</dependencies>    

<repositories>
    <!-- CUTR Android XML libraries Releases -->
    <repository>
      <id>cutr-releases</id>
      <url>https://github.com/CUTR-at-USF/cutr-mvn-repo/raw/master/releases</url>
    </repository>      
</repositories>

有关使用这些库的更多详细信息,请参阅this page

如果您想将它们直接包含在您的项目中,您可以从它们各自的目录here 下载 JAR 文件。

您还可以查看使用这些库作为工作示例的开源 "SIRI REST Client" app

编辑

如果您使用 Gradle,您的 build.gradle 将如下所示:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
    maven {
        // CUTR SNAPSHOTs
        url "https://github.com/CUTR-at-USF/cutr-mvn-repo/raw/master/snapshots"
    }
    maven {
        // CUTR Releases
        url "https://github.com/CUTR-at-USF/cutr-mvn-repo/raw/master/releases"
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"
    ...
}

dependencies {
    ...
    // Normal Jackson libraries
    compile 'com.fasterxml.jackson.core:jackson-core:2.1.2'
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.1.2'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.1.2'
    // Repackaged XML-specific libraries
    compile 'edu.usf.cutr.android.xml:jackson-dataformat-xml-android:2.1.2'
    compile 'edu.usf.cutr.android.xml:stax2-api-android:3.1.1'
    compile 'edu.usf.cutr.android.xml:stax-api-android:1.0-2'
    compile 'edu.usf.cutr.android.xml:aalto-xml-android:0.9.8'
    ...       
}

【讨论】:

  • 如果您在这里寻找用于 xml 解析的 android 兼容版本的 jackson,您可能需要阅读 this page。 Sean 很乐意提供修改后的库jackson-dataformat-xml-android。将它与jackson-corejackson-anntotationjackson-databind 一起使用,然后开心就好:)
  • 感谢@sulai 的提醒!我相信我在该库发布之前写了这篇文章,所以我会更新我上面的答案以包含它。很高兴它有用!
  • 有人有这个 pom.xml 的 Android Studio gradle equals 吗?
  • @RoundSparrowhilltx 我相信我会,我会尽快发布。
  • @SeanBarbeau 谢谢 - 到目前为止我有: compile 'edu.usf.cutr.android.xml:jackson-dataformat-xml-android:2.1.2' compile 'edu.usf.cutr.android .xml:stax2-api-android:3.1.1' 编译'edu.usf.cutr.android.xml:stax-api-android:1.0-2' 编译'edu.usf.cutr.android.xml:aalto-xml -android:0.9.8' 正在尝试使存储库 maven 链接正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-21
  • 2011-09-04
  • 2011-06-19
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
相关资源
最近更新 更多