开发者

XStream avoid collection xml element

Given a List of POJO's, if I serialize them with XStream I get:

<list>
  <开发者_JAVA技巧pojo>
     <a>a</a>
     <b>b</b>
  </pojo>
  <pojo>
     <a>a</a>
     <b>b</b>
  </pojo>
</list>

How can I do the serialization and omit the <list> </list> entries? I've used addImplicitCollection for a similar purpose but that was to omit the collection instance variable name when the collection was a member of a class being serialized.

Note: This question appears similar but not exactly relevant (I think).


These days (using XStream 1.4.1) it is possible to omit the container element from the output using "implicit collections"


You can't. Imagine that <list> node was gone - how would XStream know how to deserialize this XML? It can be list / set / array / something else entirely. Furthermore, imagine you have an object containing a list of your pojo followed by a single pojo field - they'd be jumbled together.

That said, if you have no intention of deserializing this, you can implement your own stream driver and writer akin to JSON writer that would drop the <list> for you.


XML must have a single root element, so ChssPly76 is right, but of course if you are streaming XML then you can just write each pojo one after the other (make sense from a memory perspective as well).

See http://x-stream.github.io/objectstream.html for details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜