1 16 package dlog4j.blog; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.Serializable ; 22 23 import org.apache.commons.digester.Digester; 24 import org.xml.sax.SAXException ; 25 26 30 public class TrackBackResp implements Serializable { 31 32 int error; 33 String message; 34 String encode = "UTF-8"; 35 36 public static TrackBackResp parse(InputStream in) throws IOException , SAXException { 37 Digester digester = new Digester(); 38 digester.setValidating(false); 39 digester.addObjectCreate("response", TrackBackResp.class); 40 digester.addBeanPropertySetter("response/error","error"); 41 digester.addBeanPropertySetter("response/message","message"); 42 return (TrackBackResp)digester.parse(in); 43 } 44 45 public static TrackBackResp parse(String xml) throws IOException , SAXException { 46 ByteArrayInputStream bais = new ByteArrayInputStream (xml.getBytes()); 47 Digester digester = new Digester(); 48 digester.setValidating(false); 49 digester.addObjectCreate("response", TrackBackResp.class); 50 digester.addBeanPropertySetter("response/error","error"); 51 digester.addBeanPropertySetter("response/message","message"); 52 return (TrackBackResp)digester.parse(bais); 53 } 54 55 public String toString(){ 56 StringBuffer xml = new StringBuffer (); 57 xml.append("<?xml version=\"1.0\" encoding=\""); 58 xml.append(encode); 59 xml.append("\"?>\n"); 60 xml.append("<response>\n"); 61 xml.append("\t<error>"); 62 xml.append(error); 63 xml.append("</error>"); 64 if(message!=null){ 65 xml.append("\t<message>"); 66 xml.append(message); 67 xml.append("</message>"); 68 } 69 xml.append("\n</response>"); 70 return xml.toString(); 71 } 72 73 public int getError() { 74 return error; 75 } 76 public void setError(int error) { 77 this.error = error; 78 } 79 public String getMessage() { 80 return message; 81 } 82 public void setMessage(String message) { 83 this.message = message; 84 } 85 public String getEncode() { 86 return encode; 87 } 88 public void setEncode(String encode) { 89 this.encode = encode; 90 } 91 public static void main(String [] args) throws IOException , SAXException { 92 TrackBackResp resp = new TrackBackResp(); 93 resp.setError(0); 94 System.out.println(TrackBackResp.parse(resp.toString())); 95 } 96 } 97 | Popular Tags |