将bean转换成XML字符串
package com.sinoservices.bms.bbl.rest.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ZwbShGetCustomerCaResponse")
public class BmCurrentBillResponseBean { @XmlElement(name = "Accountbillheader")
private BmCurrentBillHeaderList bmCurrentBillHeaderList;
@XmlElement(name = "ReturnMassage")
private String returnMassage;
@XmlElement(name = "ReturnStatus")
private String returnStatus; public BmCurrentBillHeaderList getBmCurrentBillHeaderList() {
return bmCurrentBillHeaderList;
}
public void setBmCurrentBillHeaderList(BmCurrentBillHeaderList bmCurrentBillHeaderList) {
this.bmCurrentBillHeaderList = bmCurrentBillHeaderList;
}
public String getReturnMassage() {
return returnMassage;
}
public void setReturnMassage(String returnMassage) {
this.returnMassage = returnMassage;
}
public String getReturnStatus() {
return returnStatus;
}
public void setReturnStatus(String returnStatus) {
this.returnStatus = returnStatus;
}
}
package com.sinoservices.bms.bch.common.util; import java.lang.reflect.Field;
import java.util.Date; import javax.xml.bind.Marshaller.Listener; public class MarshallerListener extends Listener {
public static final String BLANK_CHAR = ""; @Override
public void beforeMarshal(Object source) {
super.beforeMarshal(source);
Field[] fields = source.getClass().getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
// 获取字段上注解<pre name="code" class="java">
try {
if (f.getType() == String.class && f.get(source) == null) {
f.set(source, BLANK_CHAR);
} else if (f.getType() == Date.class && f.get(source) == null) {
f.set(source, new Date());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
package com.sinoservices.bms.bch.common.util; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; import org.apache.log4j.Logger; /**
*
* @Description XML工具类
* @author Lynch.Feng
* @Date 2018年11月30日 下午4:10:50
* @version 1.0.0
*/
public class XmlUtil {
private static final Logger LOGGER = Logger.getLogger(XmlUtil.class); /**
* Description:把java类解析成XML字符串
*
* @param object java类
* @param encoding XML编码
* @return
*/
public static String getXmlWithoutHeader(Object object) {
try {
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setListener(new MarshallerListener());
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 编码格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);// 是否省略xml头信息
ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
marshaller.marshal(object, dataStream);
return dataStream.toString("UTF-8");// "GBK"
} catch (Exception e) {
LOGGER.error("XML转换失败", e);
e.printStackTrace();
return "XML转换失败";
}
} /**
* Description:把java类解析成XML字符串
*
* @param object java类
* @param encoding XML编码
* @return
*/
public static String getXmlFromObject(Object object, String encoding) {
try {
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setListener(new MarshallerListener());
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);// 编码格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);// 是否格式化生成的xml串
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);// 是否省略xml头信息
ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
marshaller.marshal(object, dataStream);
return dataStream.toString(encoding);// "GBK"
} catch (Exception e) {
LOGGER.error("XML转换失败", e);
return "XML转换失败";
}
} /**
* Description:把java类解析成XML字符串
*
* @param clazz java类
* @param xml XML字符串
* @return
*/
public static Object getObjectFromXml(Class<?> clazz, String xml) {
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unMarshaller = context.createUnmarshaller();
return unMarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
LOGGER.error("XML转换失败", e);
return null;
}
} /**
*
* @Description SAP 返回结果转换为对象
* @param clazz
* @param xml
* @param field
* @return
*/
public static Object getSAPObject(Class<?> clazz, String xml, String field) {
String rgex = String.format("<%s>(.*?)</%s>", field, field);
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unMarshaller = context.createUnmarshaller();
Pattern pattern = Pattern.compile(rgex);
Matcher m = pattern.matcher(xml.replaceAll("(n0:)|(\\s?xmlns:n0)[^>]*|\r\n", ""));
while (m.find()) {
xml = String.format("<%s>%s</%s>", field, m.group(1), field);
}
return unMarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
LOGGER.error("XML转换失败", e);
return null;
}
} /**
* xml文件配置转换为对象
* @param xmlPath xml文件路径
* @param load java对象.Class
* @return java对象
* @throws JAXBException
* @throws IOException
*/
@SuppressWarnings("unchecked")
public static <T> T xmlToBean(String xmlPath, Class<T> load) throws JAXBException, IOException {
JAXBContext context = JAXBContext.newInstance(load);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(new StringReader(xmlPath));
} /**
* xml转换成JavaBean
* @param xml
* @param c
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
T t = null;
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
e.printStackTrace();
} return t;
} }
将bean转换成XML字符串的更多相关文章
- Java对象转换成xml对象和Java对象转换成JSON对象
1.把Java对象转换成JSON对象 apache提供的json-lib小工具,它可以方便的使用Java语言来创建JSON字符串.也可以把JavaBean转换成JSON字符串. json-lib的核心 ...
- C#中利用LINQ to XML与反射把任意类型的泛型集合转换成XML格式字符串
在工作中,如果需要跟XML打交道,难免会遇到需要把一个类型集合转换成XML格式的情况.之前的方法比较笨拙,需要给不同的类型,各自写一个转换的函数.但是后来接触反射后,就知道可以利用反射去读取一个类型的 ...
- android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件
前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xs ...
- Java将其他数据格式转换成json字符串格式
package com.wangbo.util; import java.beans.IntrospectionException; import java.beans.Introspector; i ...
- json 字符串转换成对象,对象转换成json字符串
json 字符串转换成对象,对象转换成json字符串 前端: 方法一: parseJSON方法: [注意jquery版本问题] var str = '{"name":&qu ...
- js如何将选中图片文件转换成Base64字符串?
如何将input type="file"选中的文件转换成Base64的字符串呢? 1.首先了解一下为什么要把图片文件转换成Base64的字符串 在常规的web开发过程中,大部分上传 ...
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...
- 将bean转换成键值列表
日常开发中在进行接口对接的数据传输时,有一种场景是将bean转成jsonString,这里可以将bean转换成Map再转成jsonString. 工具类如下: public static String ...
- DataTable转换成json字符串
将DataTable里面的行转换成json字符串方法: #region DataTable转为json /// <summary> /// DataTable转为json /// < ...
随机推荐
- spring boot 整合quartz ,job不能注入的问题
在使用spring boot 整合quartz的时候,新建定时任务类,实现job接口,在使用@AutoWire或者@Resource时,运行时出现nullpointException的问题.显然是相关 ...
- Java 8 Optional 类
转自:https://www.runoob.com/java/java8-optional-class.html Optional 类是一个可以为null的容器对象.如果值存在则isPresent() ...
- .net core 2.0 HTTPS request fails using HttpClient 安全错误
最近.net core 项目中遇到一个问题,通过Httpclient 访问https的接口报错,错误如下: WinHttpException: A security error occurred Sy ...
- 自然语言推断(NLI)、文本相似度相关开源项目推荐(Pytorch 实现)
Awesome-Repositories-for-NLI-and-Semantic-Similarity mainly record pytorch implementations for NLI a ...
- vs2015下编译duilib的几个问题
duilib下载地址在github 用vs2015打开,提示升级工程,确认后继续. 编译,UIGifAnim.cpp 323行报错 1>Control\UIGifAnim.cpp(324): e ...
- 计算系统中互联设备Survey
Survey of Inter-connects in computer system 姚伟峰 http://www.cnblogs.com/Matrix_Yao/ https://github.co ...
- php导出excel不使用科学计数法
在变量前后拼接上制表符 foreach($orderList as $k=>$v){ $orderList[$k]['pos_id'] = "\t".$v['pos_id'] ...
- C博客01--顺序、分支结构
1.本章学习总结 1.1 思维导图 1.2 本章学习体会及代码量学习体会 1.2.1 学习体会 经过一周的初步学习,对C语言我有了一定的认识,也体验到了代码的乐趣,这应该为我以后的学习开了一个好头.在 ...
- WMS程序部署
UI部署UI-20190107-landor-修改什么BUG.JAR162\163 APP部署 外部JSP部署 备份META这个SCHEMA
- eclipse-jee-kepler 如何设置编译compiler为1.8
最新下载了jdk1.8,想在eclipse里面用一下 jdk1.8的新特性 但是,貌似eclipse(eclipse-jee-kepler-SR2-win32-x86_64.zip)最高的编译级别为: ...