org.ibankapp.base.util.Jaxb2Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of base-util Show documentation
Show all versions of base-util Show documentation
the base moudle of ibankapp series
/*
* iBankApp
*
* License : Apache License,Version 2.0, January 2004
*
* See the LICENSE file in English or LICENSE.zh_CN in chinese
* in the root directory or .
*/
package org.ibankapp.base.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;
/**
* JAXB2应用类
*
* @author ibankapp
* @author codelder
* @since 1.0.0
*/
public class Jaxb2Util {
/**
* 将JavaBean转换为指定字符集的XML字符串.
*
* @param obj JavaBean
* @param encoding 字符集编码
* @return XML字符串
* @throws JAXBException 转换错误
*/
public static String convertToXml(Object obj, String encoding) throws JAXBException {
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);
return writer.toString();
}
/**
* 将xml转换为指定类型的JavaBean.
*
* @param xml xml字符串
* @param tclass JavaBean类型
* @param JavaBean类型
* @return JavaBean
* @throws JAXBException 转换错误
*/
@SuppressWarnings("unchecked")
public static T converyToJavaBean(String xml, Class tclass) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(tclass);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(new StringReader(xml));
}
}