webservice接口开发,旧工程中存在使用xfire开发的接口,对象转换为xml和xml转换为对象的时候需要些大量的代码,工作量很大。现在提供一个比较好的对象转换为xml的工具。
<!-- https://mvnrepository.com/artifact/commons-betwixt/commons-betwixt -->
<dependency>
<groupId>commons-betwixt</groupId>
<artifactId>commons-betwixt</artifactId>
<version>0.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
旧代码:对象转xml 及 xml转对象写法,需要写大量的转换代码, 如下:
Ret 对象
package com.gblfy;
import org.jdom2.Element;
import org.jdom2.Namespace;
public class Ret {
private String code = ""; // 如果成功返回true,失败返回false
private String message = ""; // 如果成功返回操作成功,失败则返回失败原因
private static final String RET_FORMAT = "<Ret><code>%s</code><message>%s</message></Ret>";
public String packBody() {
return String.format(RET_FORMAT, this.getCode(), this.getMessage());
}
public void unpackBody(Element element) {
Namespace nameSpace = element.getNamespace();
this.setCode(element.getChildText("code", nameSpace));
this.setMessage(element.getChildText("message", nameSpace));
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
StepMindModel 对象
package com.gblfy;
import lombok.Data;
@Data
public class StepMindModel {
private String stepMind;
private String stepName;
}
Response对象
package com.gblfy;
import lombok.Data;
import java.util.List;
@Data
public class Response {
List<StepMindModel> list;
Ret ret;
}
现在完全可以使用apache提供的包进行转换,使用jar:commons-betwixt-0.8.jar
工具转换演示代码如下:
package com.gblfy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.io.BeanWriter;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class XmlParseUtil {
//根标签 根据需求定义
private static final String XML_ROOT_NODE_NAME = "response";
//xml header头部
public static final String XML_HEADER = "<?xml version='1.0' encoding='UTF-8' ?>";
// public static final String XML_HEADER = "";
/**
* 将XML字符串 转换成 对象
*
* @param strInMsg : XML内容
* @param clazz
* @return
*/
public static Object xml2Obj(String strInMsg,
@SuppressWarnings("rawtypes") Class clazz) {
BeanReader beanReader = new BeanReader();
Object parse = null;
StringReader xmlReader = new StringReader(strInMsg);
beanReader.getXMLIntrospector().getConfiguration()
.setAttributesForPrimitives(false);
beanReader.getBindingConfiguration().setMapIDs(false);
try {
beanReader.registerBeanClass(XML_ROOT_NODE_NAME, clazz);
parse = beanReader.parse(xmlReader);
} catch (Exception e) {
log.error("", e);
}
return parse;
}
/**
* 将 对象 转换成 XML字符串
*
* @param inObj
* @return
*/
public static String obj2xml(Object inObj) {
StringWriter sw = new StringWriter();
BeanWriter beanWriter = new BeanWriter(sw);
sw.write(XML_HEADER);
try {
beanWriter.setEndOfLine("");
beanWriter.getBindingConfiguration().setMapIDs(false);
beanWriter.getXMLIntrospector().getConfiguration()
.setAttributesForPrimitives(false);
beanWriter.write(XML_ROOT_NODE_NAME, inObj);
} catch (Exception e) {
log.error("", e);
}
return sw.toString();
}
/**
* 将XML文件转换成 对象
*
* @param fileName
* @param clazz
* @return
*/
public static Object file2Object(String fileName, Class clazz) {
String fileContent = file2String(fileName);
return xml2Obj(fileContent, clazz);
}
/**
* 将XML文件转换成 对象
*
* @param file
* @param clazz
* @return
*/
public static Object file2Object(File file, Class clazz) {
String fileContent = file2tring(file);
return xml2Obj(fileContent, clazz);
}
/**
* 读取文件全部内容
*
* @param fileName
* @return
*/
private static String file2String(String fileName) {
File file = new File(fileName);
return file2tring(file);
}
/**
* 读取文件全部内容
*
* @param file
* @return
*/
private static String file2tring(File file) {
String encoding = "ISO-8859-1";
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(filecontent);
in.close();
} catch (FileNotFoundException e) {
log.error("", e);
} catch (IOException e) {
log.error("", e);
}
try {
return new String(filecontent, encoding);
} catch (UnsupportedEncodingException e) {
log.error("", e);
return null;
}
}
//----------------------------测试方法-------------------------------
public static void main(String[] args) {
Ret obj = new Ret();
obj.setCode("true");
obj.setMessage("成功");
//对象转换为xml:<response><code>true</code><message>成功</message></response>
String xmlString = XmlParseUtil.obj2xml(obj);
System.out.println("对象转换为xml:" + xmlString);
Ret obj2 = (Ret) XmlParseUtil.xml2Obj(xmlString, Ret.class);
//xml转换为对象:true==成功
System.out.println("xml转换为对象:" + obj2.getCode() + "==" + obj2.getMessage());
Response response = new Response();
List<StepMindModel> list = new ArrayList<StepMindModel>();
StepMindModel model = new StepMindModel();
model.setStepMind("同意");
model.setStepName("三级部门经理审核");
StepMindModel model2 = new StepMindModel();
model2.setStepMind("同意");
model2.setStepName("二级部门经理审核");
list.add(model);
list.add(model2);
response.setList(list);
response.setRet(obj);
//对象转换为xml: <?xml version='1.0' encoding='UTF-8' ?><response><list><StepMindModel><stepMind>同意</stepMind><stepName>三级部门经理审核</stepName></StepMindModel><StepMindModel><stepMind>同意</stepMind><stepName>二级部门经理审核</stepName></StepMindModel></list><ret><code>true</code><message>成功</message></ret></response>
System.out.println("对象转换为xml:" + XmlParseUtil.obj2xml(response));
}
}