import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/**
* @Copyright @2013
* @author 爱不留
* @E-mail: 334545190@qq.com
* @version 2013-9-26 上午10:55:20 类说明
*/
public class Test {
/**
* @param args
* @throws Throwable
*/
public static void main(String[] args) throws Throwable {
// TODO Auto-generated method stub
XStream xstream = new XStream(new DomDriver("UTF-8"));
xstream.alias("person", Person.class);
xstream.alias("phonenumber", PhoneNumber.class);
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
String xml = xstream.toXML(joe);
System.out.println(xml);
/*
* XStream xstream = new XStream(); ByteArrayOutputStream outputStream =
* new ByteArrayOutputStream();
*
* Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
*
*
*
* xstream.alias("person", Person.class); xstream.alias("phonenumber",
* PhoneNumber.class);
*
* Person joe = new Person("Joe", "Walnes"); joe.setPhone(new
* PhoneNumber(123, "1234-456")); joe.setFax(new PhoneNumber(123,
* "9999-999"));
*
* writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
*
* xstream.toXML(joe, writer); String xml
* =outputStream.toString("UTF-8"); System.out.println(xml);
*/
Person newJoe = (Person) xstream.fromXML(xml);
System.out
.println(newJoe.getLastname() + newJoe.getPhone().getNumber());
}
/*
* private WeChatTextMessage getWeChatTextMessage(String xml){ XStream
* xstream = new XStream(new DomDriver()); xstream.alias("xml",
* WeChatTextMessage.class); xstream.aliasField("ToUserName",
* WeChatTextMessage.class, "toUserName");
* xstream.aliasField("FromUserName", WeChatTextMessage.class,
* "fromUserName"); xstream.aliasField("CreateTime",
* WeChatTextMessage.class, "createTime"); xstream.aliasField("MsgType",
* WeChatTextMessage.class, "messageType"); xstream.aliasField("Content",
* WeChatTextMessage.class, "content"); xstream.aliasField("MsgId",
* WeChatTextMessage.class, "msgId"); WeChatTextMessage wechatTextMessage =
* (WeChatTextMessage)xstream.fromXML(xml); return wechatTextMessage; }
*
* private String getReplyTextMessage(String content, String weChatUser){
* WeChatReplyTextMessage we = new WeChatReplyTextMessage();
* we.setMessageType("text"); we.setFuncFlag("0"); we.setCreateTime(new
* Long(new Date().getTime()).toString()); we.setContent(content);
* we.setToUserName(weChatUser);
* we.setFromUserName("shanghaiweather");//TODO 你的公众帐号微信号 XStream xstream =
* new XStream(new DomDriver()); xstream.alias("xml",
* WeChatReplyTextMessage.class); xstream.aliasField("ToUserName",
* WeChatReplyTextMessage.class, "toUserName");
* xstream.aliasField("FromUserName", WeChatReplyTextMessage.class,
* "fromUserName"); xstream.aliasField("CreateTime",
* WeChatReplyTextMessage.class, "createTime");
* xstream.aliasField("MsgType", WeChatReplyTextMessage.class,
* "messageType"); xstream.aliasField("Content",
* WeChatReplyTextMessage.class, "content"); xstream.aliasField("FuncFlag",
* WeChatReplyTextMessage.class, "funcFlag"); String xml =xstream.toXML(we);
* return xml; }
*/
}