使用xstream对xml的写入

来源:互联网 发布:crm软件的使用方法 编辑:程序博客网 时间:2024/04/21 00:36

使用xstream很方便的将list集合转换成一个xml类型数据代码如下:

private static Student student = null;private static List<Student> stuList = null;private static XStream stream = null;private static String xml_head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";public static void init() {stream = new XStream();student = new Student();stuList = new ArrayList<Student>();student.setAge("21");student.setName("李岩");student.setSex("男");stuList.add(student);}public static void parseXml() throws Exception {init();stream.alias("student", Student.class);String xml = stream.toXML(stuList);System.out.println(xml);File file = new File("src/xml/student1.xml");FileOutputStream out = new FileOutputStream(file);BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));writer.write(xml_head+xml);writer.close();}
原创粉丝点击