jaxb解析xml

本文介绍如何使用Java将对象转换为XML,并通过自定义适配器实现日期对象的正确序列化和反序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
 }

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值