Android 中的编组和解组 xml 文件

发布于 2025-01-04 08:05:05 字数 738 浏览 3 评论 0原文

我正在尝试使用 spring android 来解析一个类似于我的 android 代码的 xml(序列化/反序列化从服务器流式传输的对象和/或将它们发送到 REST 服务):

<ROOT>
<USERNAME>user</USERNAME>
<INFOTYPE></INFOTYPE>
<DATA format='xml_field'>
<field name='field_id1'>1000</field>
<field name='field_id2'>xxx</field>
</DATA>
</ROOT>

我不知道它是否会有可能,但我真的很喜欢这个工具(spring android)。

但是,我会接受任何可以解决我的问题的简单解决方案。

ps:我已经读过这样的其他线程: 在 Android 中将 XML 文件解组为 Java 对象? 目前,这看起来是适合我的情况的最佳解决方案:http://simple.sourceforge.net

I´m trying to use spring android to parse an xml that looks like that on my android code (to serialize/deserialize objects streaming from server and/or send them in post to the REST service):

<ROOT>
<USERNAME>user</USERNAME>
<INFOTYPE></INFOTYPE>
<DATA format='xml_field'>
<field name='field_id1'>1000</field>
<field name='field_id2'>xxx</field>
</DATA>
</ROOT>

I don´t know if it would be possible, but I really like this tool (spring android).

However, I would accept any easy solution that addresses my issue.

ps: I've already read other threads like this:
Unmarshalling XML files into Java objects in Android?
and at the moment this looks like the best solution for my case: http://simple.sourceforge.net.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2025-01-11 08:05:05

基本上 Spring 参考回答你的问题:

2.2.4 对象到 XML 封送处理

Spring Android RestTemplate 中的对象到 XML 编组需要使用第三方 XML 映射
图书馆。简单 XML 序列化器用于提供这种编组
功能。

最简单的方法就是接受这个建议并选择 Simple。我自己已经使用过几次,它非常容易设置,并且有一种相当简单的方法来使用 Java POJO 声明层次结构。 (顺便说一句:不过我仍然更喜欢 JSON。:))

或者,如果您的 xml 结构像您的示例一样简单,那么使用 SAX 实现(反)序列化不需要花费很多精力。此选项可能会使您的代码更加紧凑。

The Spring reference basically answers your question:

2.2.4 Object to XML Marshaling

Object to XML marshaling in Spring Android RestTemplate requires the use of a third party XML mapping
library. The Simple XML serializer is used to provide this marshaling
functionality.

The easy way is to just pick up this suggestion and go with Simple. I've used it a couple of times myself and it's very easy to set up and has a fairly straightforward way of declaring the hierarchy using Java POJOs. (On a side note: I still prefer JSON though. :))

Alternatively it wouldn't require a lot of effort to implement (de)serializing using SAX, provided your xml structures are as simple as your example. This option will probably keep your code more compact.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文