xstream,如何将列表序列化为xml [英] xstream, how to serialize a list to xml

查看:218
本文介绍了xstream,如何将列表序列化为xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用xstream并尝试将列表序列化为XML。我需要一个输出结构像

I am using xstream and trying to serialize a List to XML. I need an output structure like

<Employees>
  <Employee></Employee>
  <Employee></Employee>
  <Employee></Employee>
</Employees>

序列化的对象将类似于

 List<Employee> 

或类Employees。我试图创建一个Employees作为

or a class Employees. I've tried to create an Employees as

public class Employees extends ArrayList<Employee>(){}

和各种其他方法,但不能让它序列化,因为我需要它。是否有一个简单的方法来做这样的事情?

and various other approaches but can't get it to serialize as I need it. Is there an easy way to do such a thing?

我的问题类似于 XStream - Root作为对象的集合,但是我想在没有包装器对象的情况下做。

My question is similar to XStream - Root as a collection of objects but I'd like to do it without a wrapper object.

推荐答案

您使用列表作为根元素?您可以对类进行别名。下面的代码生成您要查找的输出。

You're using a list as your root element? You can alias the class. The code below produces the output you're looking for.

public static void main(String[] args) {
    List<Employee> employees = new ArrayList<Employee>();
    employees.add(new Employee());
    employees.add(new Employee());
    employees.add(new Employee());
    XStream xstream = new XStream();
    xstream.alias("Employees", List.class);
    System.out.println(xstream.toXML(employees));
}

这篇关于xstream,如何将列表序列化为xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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