<?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. Java-Runoob:Java 对象和类

    ylbtech-Java-Runoob:Java 对象和类 1.返回顶部 1. Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 ...

  2. js作用域和作用域链

    作用域 首先要知道一点,js跟python都是一样的,先编译后执行 唯一的区别在于,js对于只声明,未定义的变量,报错是未定义,后面代码会有这个例子 编译的过程只有赋值和开辟空间的过程 只有函数,类才 ...

  3. jQuery笔记——DOM操作

    在 JavaScript 中,DOM 不但内容庞大繁杂,而且我们开发的过程中需要考虑更多的兼容性.扩展性.在 jQuery 中,已经将最常用的 DOM 操 作方法进行了有效封装,并且不需要考虑浏览器的 ...

  4. IDA Pro 权威指南学习笔记(七) - 其他 IDA 显示窗口

    默认情况下 IDA 不会打开这些窗口,可通过 View -> Open Subviews 命令打开 Strings 窗口(Strings) Strings 窗口中显示的是从二进制文件中提取出的一 ...

  5. Three.js黑暗中的萤火虫

    效果图 demo import './index.css'; // stats var stats; (function(){ stats = new Stats(); document.body.a ...

  6. java成神之——安全和密码

    安全和密码 加密算法 公钥和私钥加密解密 生成私钥和公钥 加密数据 解密数据 公钥私钥生成的不同算法 密钥签名 生成加密随机数 基本用法 指定算法 加密对象 SealedObject Signatur ...

  7. [原创]Spring Boot + Mybatis 简易使用指南(二)多参数方法支持 与 Joda DateTime类型支持

    前言 今天在开发练习项目时遇到两个mybatis使用问题 第一个问题是mapper方法参数问题,在参数大于一个时,mybatis不会自动识别参数命名 第二个问题是Pojo中使用Joda DateTim ...

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

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

  9. undefined&nbsp;reference&nbsp;to…

    照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误: undefined reference to 'pthread_create' undefined reference to ' ...

  10. 斯坦福CS229机器学习课程笔记 part2:分类和逻辑回归 Classificatiion and logistic regression

    Logistic Regression 逻辑回归 1.模型 逻辑回归解决的是分类问题,并且是二元分类问题(binary classification),y只有0,1两个取值.对于分类问题使用线性回归不 ...