package com.founderorder.common.util;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.founderorder.common.entity.RequestHeader;
import com.founderorder.common.entity.selectSignStatus.SelectSignStatusRequest;
import com.founderorder.common.entity.selectSignStatus.SelectSignStatusTransactionRequest;
public class JaxbUtil
{
// private static Log log = LogFactory.getLog(JaxbUtil.class);
/**
* JavaBean转换成xml 默认编码UTF-8
* @param obj
* @param writer
* @return
* @throws MyXmlConvertException
*/
// public static String convertToXml(Object obj) throws MyXmlConvertException
// {
// return convertToXml(obj, "UTF-8");
// }
/**
* JavaBean转换成xml
* @param obj
* @param encoding
* @return
* @throws MyXmlConvertException
*/
public static String convertToXml(Object obj, String encoding)
{
String result = null;
try
{
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
}
catch (Exception e)
{
// log.info("Javabean 转化为xml报文时异常!");
// throw new MyXmlConvertException(e.getMessage(), e);
}
return result;
}
/**
* JavaBean转换成xml
* @param obj JavaBean
* @param encoding 编码
* @param format 是否格式化xml串
* @param fragment 是否省xml头信息
* @return
* @throws MyXmlConvertException
*/
public static String convertToXml(Object obj, String encoding, Boolean format, Boolean fragment)
{
String result = null;
try
{
if( format == null ) format = true;
if( fragment == null ) fragment = true;
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, fragment);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
}
catch (Exception e)
{
}
return result;
}
/**
* xml转换成JavaBean
* @param xml
* @param c
* @return
* @throws JAXBException
* @throws MyXmlConvertException
*/
@SuppressWarnings("unchecked")
public static <T> T convertToJavaBean(String xml, Class<T> c)
throws JAXBException{
T t = null;
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
return t;
}
public static void main(String[] args) {
//<transaction>
// <transHeader>
// <transCode>TS01</transCode>
// <channelId>1000</channelId>
// </transHeader>
// <transBody>
// <corpCode>2050081446</corpCode>
// <accountNo>00000401200000004144</accountNo>
// </transBody>
// </transaction>
String xml = "<transBody>"+
"<corpCode>2050081446</corpCode>"+
"<accountNo>00000401200000004144</accountNo>"+
"</transBody>";
String xml1 ="<transaction><transHeader><transCode>TS01</transCode><channelId>1000</channelId></transHeader><transBody>"+
"<corpCode>2050081446</corpCode><accountNo>00000401200000004144</accountNo></transBody></transaction>";
String xml2 = "<transHeader><transCode>TS01</transCode><channelId>1000</channelId></transHeader>";
try {
SelectSignStatusRequest s = (SelectSignStatusRequest)convertToJavaBean(xml,SelectSignStatusRequest.class);
System.out.println(s.getAccountNo());
RequestHeader s2 = (RequestHeader)convertToJavaBean(xml2,RequestHeader.class);
System.out.println(s2.getChannelId());
SelectSignStatusTransactionRequest s1 = (SelectSignStatusTransactionRequest)convertToJavaBean(xml1,SelectSignStatusTransactionRequest.class);
System.out.println(s1.getSelectSignStatusRequest().getAccountNo());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
JaxbUtil.zip_jaxb
共1个文件
java:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 182 浏览量
2022-09-23
02:49:26
上传
评论
收藏 1KB ZIP 举报
温馨提示
xml转java对象,java对象转xml对象工具类
资源推荐
资源详情
资源评论
收起资源包目录
JaxbUtil.zip (1个子文件)
JaxbUtil.java 4KB
共 1 条
- 1
资源评论
局外狗
- 粉丝: 64
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功