<?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. 表空间 -- tablespace

    表空间是数据库的逻辑划分,一个表空间只能属于一个数据库.所有的数据库对象都存放在指定的表空间中.但主要存放的是表, 所以称作表空间. Oracle数据库中至少存在一个表空间,即SYSTEM的表空间. ...

  2. PyYAML和configparser模块讲解

    Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation ymal主要用于配置文件. Co ...

  3. linux安装xgboost

    在学校服务器上安装xgboost,事先我已经安装了anaconda,但是因为师兄已经装了python所以没加入到path. 网上的方法一般都要编译,另外官方的下载方法要联网..总之出了一堆错,最终还是 ...

  4. HTTP及XMLHTTP状态代码一览

    (一) HTTP 1.1支持的状态代码 100 Continue 初始的请求已经接受,客户应当继续发送请求的其余部分 101 Switching Protocols 服务器将遵从客户的请求转换到另外一 ...

  5. BurpSuite—-Spider模块(蜘蛛爬行)

    一.简介 Burp Spider 是一个映射 web 应用程序的工具.它使用多种智能技术对一个应用程序的内容和功能进行全面的清查. Burp Spider 通过跟踪 HTML 和 JavaScript ...

  6. Elasticsearch集群如何扩容机器?

    前提, Elasticsearch-2.4.3的3节点安装(多种方式图文详解)   比如,你已经成功搭建了3台机器的es集群,如我这里分别是192.168.80.10.192.168.80.11.19 ...

  7. ssh免密连接远程服务器

    ssh免密连接远程服务器 借助ssky-keygen和ssh-copy-id工具,通过4个简单的步骤实现无需输入密码登录远程Linux主机 1 生成密钥 通过内置的工具生成RSA算法加密的密钥 ssh ...

  8. 模拟linux的内存分配与回收

    模拟linux的内存分配与回收 要求 通过深入理解内存分配管理的三种算法,定义相应的数据结构,编写具体代码. 充分模拟三种算法的实现过程,并通过对比,分析三种算法的优劣. (1)掌握内存分配FF,BF ...

  9. Ant 执行 exec cmd.exe 时路径包含空格的问题

    需求描述 通过Ant脚本调用bat脚本 问题描述 bat脚本所在目录名称包含空格(space),cmd.exe调用时候报错The system cannot find the path specifi ...

  10. angularJS学习(三)——搭建学习环境

    1.安装Node.js 和Testacular 1.1. 安装Node.js及配置部分,在另一篇博文:node.js的安装里面讲到了,地址是:http://www.cnblogs.com/tianxu ...