适用于 Java 的简单 XML 序列化程序 [英] Easy XML Serializer for Java

查看:34
本文介绍了适用于 Java 的简单 XML 序列化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将 Java 对象序列化为 XML 以供 RESTful Web 服务使用的方法.我没有 XSD.

I'm looking for a way to serialize Java objects into XML for use by a RESTful web service. I don't have an XSD.

我查看了以下内容:

  1. JAXB - 相当重的类需要注解,还有一个 ObjectFactory 类和/或一个 jaxb.in​​dex 文件

  1. JAXB - fairly heavy weight with annotations required on classes and also an ObjectFactory class and/or a jaxb.index file

简单 - 需要注释但不需要其他配置类/文件.不幸的是它不能序列化集合.

Simple - requires annotations but no other config classes/files. Unfortunately it can't serialize Sets.

XStream - 不需要注释等,但不支持泛型

XStream - no annotations etc. required, but doesn't support generics

其他人有什么建议吗?

推荐答案

我会投票给 XStream.缺乏泛型支持是为其提供的灵活性付出的小代价.您还可以通过在序列化时序列化泛型类类型来轻松实现泛型,或者将其构建到域对象中.例如

My vote would be for XStream. The lack of generics support is a small price to pay for the amount of flexibility it offers. You can also easily implement generics by serializing the generic class type at serialization time, or build this into your domain objects. E.g.

   class Customer
   {
      List<Order> orders;

      public List<Order> getOrders()
      {
         return orders;
      }
   }

在流中,元素类型表示每个对象的类型.当类型是抽象类型或接口时,元素与实现类一起列出,除非已将其指定为该接口类型的默认值.(例如,ArrayList 作为静态类型 List 实例的默认值.)

In the stream, the element type denotes the type of each object. When the type is an abstract type or interface, the element listed with the implementing class, unless that has been specified as the default for that interface type. (E.g. ArrayList as the default for instances of static type List.)

泛型是 Java 中的玫瑰色眼镜"——它们并没有真正改变你所看到的,只是你如何看待它.无论是否支持泛型,这些对象都将通过网络完全相同地发送.

Generics are "rose coloured glasses" in java - they don't really change what you are seeing, just how you see it. The objects would be sent over the wire exactly the same if they were sent with generics support or not.

这篇关于适用于 Java 的简单 XML 序列化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆