XStream Beginner's Notes
Introduction : This chapter is mainly written in the development, there is the sending of XML data, which used the XStream framework for JavaBean and XML data conversion, but in each label, the required interface needs to encapsulate the previous <! [cdata[fromuser]]> things, so the use of the parser, in this chapter used xpp this analytic class, other I have not yet understood, but the XStream framework is very rich, here refers to a special case, the remaining attributes in the JavaBean to encapsulate the specific label , Createtime does not need to, thus gives the following procedure.
Need to download Xstream-1.3.1.jar package
On the other functions of xstream and annotations, many online, again do not explain more.
JavaBean
Package org.liufeng.course.message.resp;/** * Text message * * @author Liufeng * @date 2013-05-19 */public class TextMessage E Xtends Basemessage {//Reply message content private string Content;public string GetContent () {return content;} public void SetContent (String content) {content = content;}}
Package org.liufeng.course.message.resp;/** * Message base class (public account, normal user) * * @author Liufeng * @date 2013-05-19 */public cl Basemessage {//Receiver account (received OpenID) private string tousername;//developer number private string fromusername;//message creation time (integer) Private lo NG createtime;//Message type (text/music/news) private String msgtype;//bit 0x0001 is flagged when the star has just received the message private int funcflag;public String Gettousername () {return tousername;} public void Settousername (String tousername) {tousername = Tousername;} Public String Getfromusername () {return fromusername;} public void Setfromusername (String fromusername) {fromusername = Fromusername;} Public long Getcreatetime () {return createtime;} public void Setcreatetime (long createtime) {createtime = Createtime;} Public String Getmsgtype () {return msgtype;} public void Setmsgtype (String msgtype) {msgtype = Msgtype;} public int Getfuncflag () {return funcflag;} public void Setfuncflag (int funcflag) {funcflag = Funcflag;}}
Tool class
Package Org.liufeng.course.util;import Java.io.inputstream;import Java.io.writer;import java.util.Date;import Java.util.hashmap;import Java.util.list;import Java.util.map;import Javax.servlet.http.httpservletrequest;import Org.dom4j.document;import Org.dom4j.element;import Org.dom4j.io.saxreader;import Org.liufeng.course.message.resp.article;import Org.liufeng.course.message.resp.musicmessage;import Org.liufeng.course.message.resp.newsmessage;import Org.liufeng.course.message.resp.textmessage;import Com.thoughtworks.xstream.xstream;import Com.thoughtworks.xstream.core.util.quickwriter;import Com.thoughtworks.xstream.io.hierarchicalstreamwriter;import Com.thoughtworks.xstream.io.xml.PrettyPrintWriter; Import com.thoughtworks.xstream.io.xml.xppdriver;/** * Message tool classes */public class Messageutil {/** * return message type: text */public static F inal String resp_message_type_text = "text";/** * Text message object converted to XML * alias aliases, if not, the root node will face as com.pacage.textMessage form * @param t Extmessage text Message Object * @return XML */public static String textmessagetoxml (TextMessage textmessage) {xstream.alias ("xml", Textmessage.getclass ()); return Xstream.toxml (TextMessage);} public static void Main (string[] args) {TextMessage textmessage = new TextMessage (); Textmessage.settousername ("Fromuser "); Textmessage.setfromusername (" Touser "); Textmessage.setcreatetime (new Date (). GetTime ()); Textmessage.setmsgtype (Messageutil.resp_message_type_text); Textmessage.setfuncflag (0); String XML = new Messageutil (). Textmessagetoxml (TextMessage); SYSTEM.OUT.PRINTLN (XML);} /** * Extends XStream so that it supports CDATA blocks */private static XStream XStream = new XStream (new Xppdriver () {public hierarchicalstreamwriter Createwriter (Writer out) {return new Prettyprintwriter (out) {////For all XML node conversions added CDATA Tag Boolean = true; String createtime = ""; @SuppressWarnings ("Unchecked") public void Startnode (string name, Class clazz) {if (name!=null &&name.equals ("Createtime")) {createtime = "createtime";} Else{createtime=name;} Super.startnode (name, clazz);} protected void WriteText (QuicKwriter writer, String text) {if (Cdata&&!createtime.equals ("Createtime")) {Writer.write ("<![ Cdata["); Writer.write (text); Writer.write ("]]> ");} else {writer.write (text);}}}});}
XStream provides special parsing