marshalling and unmarshalling xml file in android

2019-08-23 02:07发布

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.

1条回答
Explosion°爆炸
2楼-- · 2019-08-23 02:33

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.

查看更多
登录 后发表回答