1.xml报文转json
//接收xml报文转json
//orgmsg xml二进制数组,transcode 接口名称,每一个接口一个取值规则,secuno 发起方id
//接收xml报文
Sting message = new String(orgmsg,"GB18030");
//定义map接收转换后的报文
Map<String,String> json = new HashMap<>();
try{
//根据接口获取解包配置
//BaseXpathConf 存放xml报文路径 对应的jsonkey配置;
BaseXpathConf xpathConf = confs.get(transcode);
if(xpathConf == null){
//抛出异常
}
//文本对象接收xml报文
Document doc = DocumentHelper.parseText(message);
//循环xpathConf ,解包配置unpack,转为json对象
//例如,unpach放的key:xml标签路径,value:jsonkey
Map<String,String> unpackConf = xpathConf.getUnpachConf();
//循环解包
for(Map.Entry<String,String> conf:unpachConf.entrySet){
String value = getValueByRequest(doc,conf.getKey);
if(value != null){
json.put(conf.getValue(),value);
}
}
}
private String getValueByRequest(Document doc,String key){
final List list = doc.selectNodes(key);
if(list == null){
return null;
}
//集合中只有一个值 的处理方案
if(list.size() ==1){
if(list.get(0) instanceof Node){
return ((Node)list.get(0)).getText();
}else if(list.get(0) instanceof String){
return list.get(0).toString();
}
}
//集合中存在多个值的处理方案
List<String> res = new ArrayList<>();
Object item = null;
for(int i=-0;i<list.size();i++){
item = list.get(i);
if(list.get(0) instanceof Node){
res.add(((Node)item).getText());
}else if(list.get(0) instanceof String){
res.add(item.toString());
}
}
return JSON.toJSONString(res);
}
2.json转xml
//根据交易吗获取解包配置
//BaseXpathConf 存放xml报文路径对应的jsonkey,
//例如:加包:pach("xml路径","josnkey");
BaseXpathConf xpathConf = confs.get(transcode);
if(xpathConf == null){
//抛出异常
}
//创建文本对象
Document doc = DocumentHelper.createDocument();
//获取xml报文模板,xml报文路径和jsonkey的对应关系;
Map<String,String> packConf = xpath.getPackConf();
for(Map.Entry<String,String> conf:packConf.entrySet()){
//从json对象中,根据pack配置的jsonkey,获取属性值
Object value = json.get(conf.getValue());
if(value != null){
DocumentHelper.makeElement(doc,conf.getkey()).setText(value.toString());
}else{
DocumentHelper.makeElement(doc,conf.getkey()).setText(“”);
}
}
//xml报文转二进制数组
byte[] messageBytes = doc.getRootElement().asXML().getByte("UTF-8");
byte[] mesTextBytes = new String(messageBytes,"UTF-8").getBytes("GB18030");
//拼接xml报文头标签,属性,例如:<SMTF len = "11" seq = "1">
int len =12+mesTextBytes.length;
//开始组装
StringBUilder sh =new StringBuilder("<SMIF len =\"">");
sh.append(String.format("%05d",len));
sh.append("\" seq = \"1"");
sh.append("\">");
//拼接xml报文体
sh.append(new String(mesTextBytes,"gbk") );
sh.append("</SMIF>");
return sb.toString().getBytes("gbk");
额外补充,配置的xml模板,以及$参数动态匹配的方式
import java.io.StringWriter;
import java.util.Map;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
//参数:
Map dataMap = new HashMap();
String name = DATABLOCK.SERVERPARAM.NAME;
String detail = DATABLOCK.SERVERPARAM.DETAIL;
dataMap.put("Name",name);
dataMap.put("Detail",detail);
//获取模板
String templateContent = DATABLOCK.SERVER_RETURN_PARAM.DATA.TEMPLAGE;
//调用方法
Tempt tempt = new Tempt();
String result = tempt.getText(dataMap,templateContent);
//上述字符串就可以通过转成map
//封装方法
public class Tempt{
public String getText(Map dataMap, String templateContent){
String restring = "";
try {
//模板配置
Configuration cfg = new Configuration();
//字符串模板加载器
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate("templateName", templateContent);
cfg.setTemplateLoader(stringLoader);
//获取模板
Template template = cfg.getTemplate("templateName", "utf-8");
//模板解析输出
StringWriter writer = new StringWriter();
//执行模板解析处理
template.process(dataMap, writer);
//输出结果
restring=writer.toString();
writer.close();
System.out.println(restring);
return restring;
} catch (Exception e) {
e.printStackTrace();
}
return restring;
}
}
希望能够帮助到你们