You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中可搭配Retrofit使用的新型XmlConverter有哪些?

Android中Retrofit可用的新型XML转换库(2024年更新)

针对四年前的问题,目前有几个更适配现代Android开发(支持Kotlin、Jetpack组件、高版本系统)的XML转换库可以搭配Retrofit使用:

1. Moshi-XML

作为Square官方生态的一员,Moshi-XML完美适配Retrofit,同时继承了Moshi在Kotlin数据类、空安全处理上的优势,支持协程和Android 10+系统。

  • 依赖配置:
implementation 'com.squareup.moshi:moshi:1.15.0'
implementation 'com.squareup.moshi:moshi-xml:0.18.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
  • Retrofit配置示例:
val moshiXml = MoshiXml.Builder().build()
val retrofit = Retrofit.Builder()
    .baseUrl("YOUR_BASE_URL")
    .addConverterFactory(MoshiXmlConverterFactory.create(moshiXml))
    .build()

2. 更新版SimpleXmlConverter

旧版SimpleXml曾是主流选择,现在的维护分支已经适配了Android 13+,支持Kotlin数据类的注解解析,修复了旧版本的内存泄漏问题。

  • 依赖配置:
implementation 'com.github.nekocode:retrofit2-converter-simplexml:2.1.0'
  • Retrofit配置示例:
val retrofit = Retrofit.Builder()
    .baseUrl("YOUR_BASE_URL")
    .addConverterFactory(SimpleXmlConverterFactory.create())
    .build()

注意:使用时需要给数据类添加@Root、@Element等注解来映射XML节点

3. Jackson XML

Jackson作为老牌解析库,其XML模块现在对Android的兼容性大幅提升,支持复杂XML结构(命名空间、嵌套节点、自定义序列化),适合处理复杂的XML接口返回。

  • 依赖配置:
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.15.2'
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'
  • Retrofit配置示例:
val xmlMapper = XmlMapper()
val retrofit = Retrofit.Builder()
    .baseUrl("YOUR_BASE_URL")
    .addConverterFactory(JacksonConverterFactory.create(xmlMapper))
    .build()

这些库都经过了近年的维护更新,适配了现代Android开发的需求,相比四年前的方案在稳定性、Kotlin支持、系统兼容性上都有明显提升。

内容的提问来源于stack exchange,提问作者Void

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 01:35:45