<?xml version="1.0" encoding="GBK" standalone="no"?><package><pub><txcode>2013</txcode><appno>999</appno></pub><req><devid>0093002</devid><tellerno>admin</tellerno><type>1</type><cardtype>02</cardtype></req></package>

具体实现原理:类的toString()方法里面进行装配:

参考代码:

public class ServerMessage {

    /** 交易码(必须) */
private String txcode = null;
/** 返回码 */
private String retcode = null;
/** 返回信息 */
private String retmsg = null;
/** 应用号(必须) */
private String appno = null; /** 设备编号(必须) */
private String devid = null;
/** 柜员号 */
private String tellerno = null;
/** 卡号 */
private String cardno = null;
private String cardno2 = null; /** 持卡人姓名 */
private String perName;
/** 持卡人身份证 */
private String perNo;
/** 社保卡号 */
private String siCardNo; /**
* 盘库类型(1全部;2按指定层指定槽盘层;3从指定位置全部;4指定槽)
* 下载登记簿类型(01卡登记簿02进卡登记簿03取卡登记簿04进取卡登记簿05故障登记簿06故障卡槽登记簿07进取卡统计报表)
*/
private String type = null; // 出卡类型 或者进卡类型
/** 盘库——第几层开始盘 */
private String num = null;
/** 盘库——第几卡槽开始盘 */
private String position = null;
/** 盘库——序号 */
private String serial = null;
/** 文件路径 */
private String filepath = null;
/** 操作日期 */
private String operDate = null;
private boolean reqStatus = false; // <req>标签标记
/** 授权柜员号 */
private String author = null;
/** 开始时间 */
private String startDate = null;
/** 结束时间 */
private String endDate = null;
/** 卡种 */
private String cardtype = null; public ServerMessage() {
this.appno = ProjectConfig.getInstance().getAppno(); // 获取应用号
} public ServerMessage(String txcode, String devid) {
this.txcode = txcode;
this.devid = devid;
this.appno = ProjectConfig.getInstance().getAppno(); // 获取应用号
} public ServerMessage(String txcode, String devid, String tellerno) {
this(txcode, devid);
this.tellerno = tellerno;
} /**
* 创建同步请求消息
*/
public static ServerMessage createSyncMessage(String txcode, String devid, String tellerno) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setReqStatus(true);
return msg;
} /**
* 创建批量取卡消息
*/
public static ServerMessage createTakeCardMessage(String txcode, String devid, String tellerno, String cardno, String perNo, String perName, String siCardNo, String type) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setType(type);
msg.setCardno(cardno);
msg.setPerNo(perNo);
msg.setPerName(perName);
msg.setSiCardNo(siCardNo);
msg.setReqStatus(true);
return msg;
} /**
* 创建盘库消息
*/
public static ServerMessage createPankuMessage(String txcode, String devid, String tellerno, String type, String num, String position,
String serial, String filepath) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setType(type);
msg.setNum(num);
msg.setPosition(position);
msg.setSerial(serial);
msg.setFilepath(filepath);
msg.setReqStatus(true);
return msg;
} /**
* 创建上传终端日志消息
*/
public static ServerMessage createUploadLogMessage(String txcode, String devid, String tellerno, String operDate, String filepath) {
String date = operDate.replace("-", ""); // 接口的日期格式是yyyyMMdd
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setOperDate(date);
msg.setFilepath(filepath);
msg.setReqStatus(true);
return msg;
} /**
* 创建状态消息
*/
public static ServerMessage createReplyMessage(String txcode, String retcode, String retmsg) {
ServerMessage msg = new ServerMessage();
msg.setTxcode(txcode);
msg.setRetcode(retcode);
msg.setRetmsg(retmsg);
return msg;
} /**
* 创建状态消息(进卡/取卡)
*/
public static ServerMessage createReplyMessage(String txcode, String retcode, String retmsg, String cardno2) {
ServerMessage msg = new ServerMessage();
msg.setTxcode(txcode);
msg.setRetcode(retcode);
msg.setRetmsg(retmsg);
msg.setCardno2(cardno2);
return msg;
} /**
* 创建手工登记消息
*/
public static ServerMessage createRegisterMessage(String txcode, String devid, String tellerno, String cardno, String author, String type) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setCardno(cardno);
msg.setAuthor(author);
msg.setType(type);
msg.setReqStatus(true);
return msg;
} /**
* 创建下载登记簿消息
*/
public static ServerMessage createDownInfoMessage(String txcode, String devid, String tellerno, String filepath, String type, String startDate,
String endDate) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setFilepath(filepath);
msg.setType(type);
msg.setStartDate(startDate);
msg.setEndDate(endDate);
msg.setReqStatus(true);
return msg;
} /**
* 创建盘点消息
*/
public static ServerMessage createPandianMessage(String txcode, String devid, String tellerno, String type, String num, String position,
String serial, String filepath) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setType(type);
msg.setNum(num);
msg.setPosition(position);
msg.setSerial(serial);
msg.setFilepath(filepath);
msg.setReqStatus(true);
return msg;
} /**
* 创建根据卡槽取卡消息
*/
public static ServerMessage createTakeCardBySlotMessage(String txcode, String devid, String tellerno, String num, String position) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setNum(num);
msg.setPosition(position);
msg.setReqStatus(true);
return msg;
} /**
* 创建进卡消息
*/
public static ServerMessage createCardInMessage(String txcode, String devid, String tellerno, String type, String cardtype) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
msg.setType(type);
msg.setCardtype(cardtype);
msg.setReqStatus(true);
return msg;
} /**
* 创建取消进卡消息
*/
public static ServerMessage createUnCardInMessage(String txcode, String devid, String tellerno, String type) {
ServerMessage msg = new ServerMessage(txcode, devid, tellerno);
//msg.setType(type);
msg.setReqStatus(true);
return msg;
} public String getCardtype() {
return cardtype;
} public void setCardtype(String cardtype) {
this.cardtype = cardtype;
} public String getTxcode() {
return txcode;
} public void setTxcode(String txcode) {
this.txcode = txcode;
} public String getAppno() {
return appno;
} public void setAppno(String appno) {
this.appno = appno;
} public String getDevid() {
return devid;
} public void setDevid(String devid) {
this.devid = devid;
} public String getTellerno() {
return tellerno;
} public void setTellerno(String tellerno) {
this.tellerno = tellerno;
} public String getCardno() {
return cardno;
} public void setCardno(String cardno) {
this.cardno = cardno;
} public String getPerName() {
return perName;
} public void setPerName(String perName) {
this.perName = perName;
} public String getPerNo() {
return perNo;
} public void setPerNo(String perNo) {
this.perNo = perNo;
} public String getSiCardNo() {
return siCardNo;
} public void setSiCardNo(String siCardNo) {
this.siCardNo = siCardNo;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public String getNum() {
return num;
} public void setNum(String num) {
this.num = num;
} public String getPosition() {
return position;
} public void setPosition(String position) {
this.position = position;
} public String getSerial() {
return serial;
} public void setSerial(String serial) {
this.serial = serial;
} public String getFilepath() {
return filepath;
} public void setFilepath(String filepath) {
this.filepath = filepath;
} public String getRetcode() {
return retcode;
} public void setRetcode(String retcode) {
this.retcode = retcode;
} public String getRetmsg() {
return retmsg;
} public void setRetmsg(String retmsg) {
this.retmsg = retmsg;
} public String getOperDate() {
return operDate;
} public void setOperDate(String operDate) {
this.operDate = operDate;
} public boolean isReqStatus() {
return reqStatus;
} public void setReqStatus(boolean reqStatus) {
this.reqStatus = reqStatus;
} public String getCardno2() {
return cardno2;
} public void setCardno2(String cardno2) {
this.cardno2 = cardno2;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public String getStartDate() {
return startDate;
} public void setStartDate(String startDate) {
this.startDate = startDate;
} public String getEndDate() {
return endDate;
} public void setEndDate(String endDate) {
this.endDate = endDate;
} public String toXml() {
StringBuilder buf = new StringBuilder();
buf.append("<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\"?>");
buf.append("<package>");
buf.append("<pub>");
buf.append("<txcode>").append(txcode).append("</txcode>");
buf.append("<appno>").append(appno).append("</appno>");
if (retcode != null && retcode.length() > 0) {
buf.append("<retcode>").append(retcode).append("</retcode>");
}
if (retmsg != null && retmsg.length() > 0) {
buf.append("<retmsg>").append(retmsg).append("</retmsg>");
}
buf.append("</pub>");
if (reqStatus) {
buf.append("<req>");
if (devid != null && devid.length() > 0) {
buf.append("<devid>").append(devid).append("</devid>");
}
if (tellerno != null && tellerno.length() > 0) {
buf.append("<tellerno>").append(tellerno).append("</tellerno>");
}
if (cardno != null && cardno.length() > 0) {
buf.append("<cardno>").append(cardno).append("</cardno>");
}
if (type != null && type.length() > 0) {
buf.append("<type>").append(type).append("</type>");
}
if (cardtype != null && cardtype.length() > 0) {
buf.append("<cardtype>").append(cardtype).append("</cardtype>");
} if (perNo != null && perNo.length() > 0) {
buf.append("<idandname>").append(perNo).append("|").append(perName).append("</idandname>");
}
if (siCardNo != null && siCardNo.length() > 0) {
buf.append("<sicardno>").append(siCardNo).append("</sicardno>");
} if (num != null && num.length() > 0) {
buf.append("<num>").append(num).append("</num>");
}
if (position != null && position.length() > 0) {
buf.append("<position>").append(position).append("</position>");
}
if (serial != null && serial.length() > 0) {
buf.append("<serial>").append(serial).append("</serial>");
}
if (operDate != null && operDate.length() > 0) {
buf.append("<operDate>").append(operDate).append("</operDate>");
}
if (filepath != null && filepath.length() > 0) {
buf.append("<filepath>").append(filepath).append("</filepath>");
}
if (author != null && author.length() > 0) {
buf.append("<author>").append(author).append("</author>");
}
if (startDate != null && startDate.length() > 0) {
buf.append("<startDate>").append(startDate).append("</startDate>");
}
if (endDate != null && endDate.length() > 0) {
buf.append("<endDate>").append(endDate).append("</endDate>");
}
buf.append("</req>");
}
if (cardno2 != null && cardno2.length() > 0) {
buf.append("<ans><cardno>").append(cardno2).append("</cardno></ans>");
}
buf.append("</package>");
return buf.toString();
} public String toString() {
return toXml();
} // public static void main(String[] args) {
// ServerMessage msg = createDownInfoMessage("2009", "001", "400085470", "/aaa/bbb/ccc", "01", "2015-11-12", "2015-11-14");
// System.out.println(msg);
// } }

将某个类封装成XML形式返回的更多相关文章

  1. java类封装成dll

    @参考文章1,@参考文章2,@参考文章3 1,建立测试类,注意英文注释部分,用汉语直接编译会乱码 public class Hello { //native method is used for ca ...

  2. ybatis中查询出多个以key,value的属性记录,封装成一个map返回的方法

    可以采用值做映射,也可以不采用映射方式 <resultMap id="configMap" type="java.util.Map" > <r ...

  3. 把jQuery的类、插件封装成seajs的模块的方法

    这篇文章主要介绍了把jQuery的类.插件封装成seajs的模块的方法,需要的朋友可以参考下 注:本文使用的seajs版本是2.1.1 一.把Jquery封装成seajs的模块 define(func ...

  4. 将bean转换成XML字符串

    package com.sinoservices.bms.bbl.rest.bean; import javax.xml.bind.annotation.XmlAccessType; import j ...

  5. C#实体类(复杂类)与XML互相转换

    实体类转换成XML方法: 将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化 public static string XmlSerialize<T ...

  6. java将类和函数封装成jar,然后在别的项目中使用这个jar包

    本来想用idea安装的,不过用maven生成后发现jar有20,30M肯定不对,后来还是用eclipse生成了,方便很多 环境: eclipse luna,jdk1.8_112 1.生成jar包,首先 ...

  7. java将类和函数封装成jar

    本来想用idea安装的,不过用maven生成后发现jar有20,30M肯定不对,后来还是用eclipse生成了,方便很多 环境: eclipse luna,jdk1.8_112 1.生成jar包,首先 ...

  8. 为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?

    既然用Hibernate框架访问管理持久层,那为何又提到用Spring来管理以及整合Hibernate呢?把SessionFactory,Session,Transcational封装成包含crud的 ...

  9. Directx11学习笔记【二】 将HelloWin封装成类

    我们把上一个教程的代码封装到一个类中来方便以后的使用. 首先新建一个空工程叫做MyHelloWin,添加一个main.cpp文件,然后新建一个类叫做MyWindow,将于窗体有关的操作封装到里面 My ...

随机推荐

  1. PHP面向对象深入研究之【组合模式与装饰模式】

    组合模式 定义:组合模式定义了一个单根继承体系,使具有截然不同职责的集合可以并肩工作. 一个军队的案例, <?php abstract class Unit { // 个体 abstract f ...

  2. java成神之——HttpURLConnection访问api

    HttpURLConnection 访问get资源 访问post资源 访问Delete资源 获取状态码 结语 HttpURLConnection 访问get资源 HttpURLConnection c ...

  3. hadoop中unhealthynodes的问题解决

    在yarn-site.xml中加入如下配置 <property>     <name>yarn.nodemanager.disk-health-checker.min-heal ...

  4. express源码学习

    终于腾出手来学习express.express在node.js中一株独秀.好像任何一种有主导的托管平台的语言,都出现这现象--马太效应.express是社区的共同孩子,里面聚集上社区最好的常用模块.从 ...

  5. 【原】Coursera—Andrew Ng机器学习—Week 2 习题—Linear Regression with Multiple Variables 多变量线性回归

    Gradient Descent for Multiple Variables [1]多变量线性模型  代价函数 Answer:AB [2]Feature Scaling 特征缩放 Answer:D ...

  6. std::mutex与pthread mutex区别

    Linux下 pthread mutex * PTHREAD_MUTEX_TIMED_NP,这是缺省值,也就是普通锁.当一个线程加锁以后,其余请求锁的线程将形成一个等待队列,并在解锁后按优先级获得锁. ...

  7. 2014蓝桥杯B组初赛试题《六角填数》

    题目描述: 如图[1.png]所示六角形中,填入1~12的数字.     使得每条直线上的数字之和都相同.     图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交 ...

  8. 关于前端XSS攻击、短信轰炸等问题

    关于前端的XSS攻击: 这里有一篇Ryf老师的写的关于使用  Content Security Policy (简称 CSP 防御)来防御xss攻击,简单来说就是设置白名单,告诉浏览器哪些链接.内容是 ...

  9. 14.UNION 和 UNION ALL 操作符

    union all   union UNION 操作符用于合并两个或多个 SELECT 语句的结果集.请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同 ...

  10. Django框架 之 logging配置

    Django框架 之 logging配置 logging配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...