import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.whclx.framework.common.exception.BusinessException;
public class BeanXmlUtil {
/**
* 将对象解析成XML
* @param obj
* @return
*/
public static String format(Object obj) {
try {
JAXBContext ctx = JAXBContext.newInstance(obj.getClass());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Marshaller m = ctx.createMarshaller();
m.setProperty(Marshaller.JAXB_ENCODING, "GBK");
m.marshal(obj, baos);
return baos.toString();
} catch (JAXBException e) {
throw new IllegalStateException(e);
}
}
/**
* 将对象解析成XML
* @param obj
* @return
*/
public static String formatUseUtf8(Object obj) {
try {
JAXBContext ctx = JAXBContext.newInstance(obj.getClass());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Marshaller m = ctx.createMarshaller();
m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(obj, baos);
return baos.toString();
} catch (JAXBException e) {
throw new IllegalStateException(e);
}
}
/**
* 将XML解析成对象
*/
public static Object parse(String xml, Class<?> clazz) {
xml = xml.replace("encoding=\"GBK\"", "encoding=\"UTF-8\"");
try {
JAXBContext ctx = JAXBContext.newInstance(clazz);
ByteArrayInputStream bis = new ByteArrayInputStream( xml.getBytes("UTF-8") );
Unmarshaller um = ctx.createUnmarshaller();
return um.unmarshal(bis);
} catch (JAXBException e) {
throw new IllegalStateException(e);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new BusinessException("不支持编码异常");
}
}
/**
* 将XML解析成对象
*/
public static Object parseGBK(String xml, Class<?> clazz) {
xml = xml.replace("encoding=\"UTF-8\"", "encoding=\"GBK\"");
try {
JAXBContext ctx = JAXBContext.newInstance(clazz);
ByteArrayInputStream bis = new ByteArrayInputStream( xml.getBytes("GBK") );
Unmarshaller um = ctx.createUnmarshaller();
return um.unmarshal(bis);
} catch (JAXBException e) {
throw new IllegalStateException(e);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new BusinessException("不支持编码异常");
}
}
}
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
public class JavaDateXmlAdapter extends XmlAdapter<String, Date> {
/* (non-Javadoc)
* @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
*/
@Override
public Date unmarshal(String v) throws Exception {
return DateUtils.parseDate(v, "yyyy-MM-dd HH:mm:ss");
}
/* (non-Javadoc)
* @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
*/
@Override
public String marshal(Date v) throws Exception {
return DateFormatUtils.format(v, "yyyy-MM-dd HH:mm:ss");
}
}