Android XML 对象序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3587504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:51:40  来源:igfitidea点击:

Android XML Object Serialization

androidxmlserialization

提问by Rodrigo Gama

I would like to serialize an object to XML inside Android.

我想在 Android 中将对象序列化为 XML。

Any libs suggested?

任何库建议?

PS: Already tried XStream, but it doesn't serialize enums correctly with Android. The issue is here: Serialization problem with Enums at Android

PS:已经尝试过 XStream,但它没有使用 Android 正确序列化枚举。问题在这里:Android Enums 的序列化问题

采纳答案by Cheryl Simon

Unfortunately I don't think there is a good solution for xml serialization of objects in Android yet. Most of the existing solutions are too heavyweight for use in mobile apps, and depend on things Android doesn't support.

不幸的是,我认为 Android 中对象的 xml 序列化还没有一个好的解决方案。大多数现有的解决方案对于在移动应用程序中使用来说太重了,并且依赖于 Android 不支持的东西。

For an overview of XML options, see working with xml in Android.

有关 XML 选项的概述,请参阅在 Android 中使用 xml

回答by Grant

Have you tried simple http://simple.sourceforge.net/it is working well for me, correctly handles enums, the jar is quite large though 320k.

您是否尝试过简单的http://simple.sourceforge.net/它对我来说效果很好,可以正确处理枚举,虽然 320k,但 jar 相当大。

回答by Jamie Little

It's not XML, but you might want to use Google's GSON library to serialize objects. It's what they recommend in the their documentation as an alternative to Serializable:

它不是 XML,但您可能希望使用 Google 的 GSON 库来序列化对象。这是他们在文档中推荐的替代Serializable 的方法

JSON is concise, human-readable and efficient. Android includes both a streaming API and a tree API to read and write JSON. Use a binding library like GSONto read and write Java objects directly.

JSON 简洁、易读且高效。Android 包含流 API 和树 API 来读取和写入 JSON。使用GSON 之类的绑定库直接读写 Java 对象。