日期:2014-05-16  浏览次数:20335 次

ExtJS与Struts 2框架整合

一、 XMLJSON的生成

示例一:将Java对象转换成XML数据

package com.ibeifeng.xstream;

?

import com.thoughtworks.xstream.XStream;

import com.thoughtworks.xstream.io.xml.DomDriver;

?

public class TestXStream {

?????? public static void main(String[] args) {

????????????? Person person = new Person();

????????????? person.setName("张三");

????????????? person.setAge(20);

????????????? person.setSex(true);

????????????? DomDriver domDriver = new DomDriver();

????????????? XStream xtream = new XStream(domDriver);

????????????? xtream.alias("person", Person.class);

????????????? String xml = xtream.toXML(person);

????????????? System.out.println(xml);

?????? }

}

示例二:将Java对象集合转换成XML数据

package com.ibeifeng.xstream;

?

import java.util.ArrayList;

import java.util.List;

?

import com.thoughtworks.xstream.XStream;

import com.thoughtworks.xstream.io.xml.DomDriver;

?

public class TestXStreamList {

?????? public static void main(String[] args) {

????????????? List<Person> persons = new ArrayList<Person>();

????????????? Person person1 = new Person();

????????????? person1.setName("张三");

????????????? person1.setAge(20);

????????????? person1.setSex(true);

?????????????

????????????? Person person2 = new Person();

????????????? person2.setName("李四");

????????????? person2.setAge(30);

????????????? person2.setSex(false);

?????????????

????????????? persons.add(person1);

????????????? persons.add(person2);

?????????????

????????????? DomDriver domDriver = new DomDriver();

????????????? XStream xtream = new XStream(domDriver);