我有同样的问题,想在解析 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-android、aalto-xml、stax-api 和 stax2-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'
...
}