javeBean与xml之间的相互转换





import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *<pre><b><font color="blue">JaxbUtil</font></b></pre>
 *
 *<pre><b>&nbsp;--描述说明--</b></pre>
 * <pre></pre>
 * <pre>
 * <b>--样例--</b>
 *   JaxbUtil obj = new JaxbUtil();
 *   obj.method();
 * </pre>
 * JDK版本:JDK1.6
 * @author  <b>fengyz</b>
 */
public class JaxbUtil {
    /**
     * JavaBean转换成xml
     * 默认编码UTF-8
     * @param obj
     * @param writer
     * @return 
     */ 
    public static String convertToXml(Object obj) { 
        return convertToXml(obj, "UTF-8"); 
    } 

    /**
     * JavaBean转换成xml
     * @param obj
     * @param encoding 
     * @return 
     */ 
    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, false); 
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            StringWriter writer = new StringWriter(); 
            marshaller.marshal(obj, writer);
            result = writer.toString(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 

        return result; 
    } 

    /**
     * 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; 
    } 
}


Test


import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 *<pre><b><font color="blue">Country</font></b></pre>
 *
 *<pre><b>&nbsp;--描述说明--</b></pre>
 * <pre></pre>
 * <pre>
 * <b>--样例--</b>
 *   Country obj = new Country();
 *   obj.method();
 * </pre>
 * JDK版本:JDK1.6
 * @author  <b>fengyz</b>
 */
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name = "country")  //
@XmlType(propOrder = { "name", "provinceList" }) 
public class Country { 

    @XmlElement(name = "country_name") 
    private String name; 

    @XmlElementWrapper(name = "provinces") 
    @XmlElement(name = "province") 
    private List<Province> provinceList; 

    /**
     * @return the name
     */ 
    public String getName() { 
        return name; 
    } 

    /**
     * @return the provinceList
     */ 
    public List<Province> getProvinceList() { 
        return provinceList;
    } 

    /**
     * @param name the name to set
     */ 
    public void setName(String name) { 
        this.name = name; 
    } 

    /**
     * @param provinceList the provinceList to set
     */ 
    public void setProvinceList(List<Province> provinceList) { 
        this.provinceList = provinceList; 
    } 

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */ 
    @Override 
    public String toString() { 
        return "Country [name=" + name + ", provinceList=" + provinceList + "]"; 
    } 





import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

/**
 *<pre><b><font color="blue">Province</font></b></pre>
 *
 *<pre><b>&nbsp;--描述说明--</b></pre>
 * <pre></pre>
 * <pre>
 * <b>--样例--</b>
 *   Province obj = new Province();
 *   obj.method();
 * </pre>
 * JDK版本:JDK1.6
 * @author  <b>fengyz</b>
 */
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(propOrder = { "name", "provCity" }) 
public class Province { 

    @XmlElement(name = "province_name") 
    private String name; 

    @XmlElement(name = "prov_city") 
    private String provCity; 

    /**
     * @return the provCity
     */ 
    public String getProvCity() { 
        return provCity; 
    } 

    /**
     * @param provCity the provCity to set
     */ 
    public void setProvCity(String provCity) { 
        this.provCity = provCity; 
    } 

    /**
     * @return the name
     */ 
    public String getName() { 
        return name; 
    } 

    /**
     * @param name the name to set
     */ 
    public void setName(String name) { 
        this.name = name; 
    } 

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */ 
    @Override 
    public String toString() { 
        return "Province [name=" + name + ", provCity=" + provCity + "]"; 
    } 




import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBException;

import org.junit.Test;

import com.dragonsoft.kyry.sys.service.impl.BusService;
import com.dragonsoft.kyry.sys.vo.HcTaskParam;

/**
 *<pre><b><font color="blue">JaxbTest3</font></b></pre>
 *
 *<pre><b>&nbsp;--描述说明--</b></pre>
 * <pre></pre>
 * <pre>
 * <b>--样例--</b>
 *   JaxbTest3 obj = new JaxbTest3();
 *   obj.method();
 * </pre>
 * JDK版本:JDK1.6
 * @author  <b>fengyz</b>
 */
public class JaxbTest3 { 

    /**
     * @throws JAXBException
     */ 
    @Test 
    public void showMarshaller() { 
        Country country = new Country(); 
        country.setName("中国"); 

        List<Province> list = new ArrayList<Province>(); 
        Province province = new Province(); 
        province.setName("江苏省"); 
        province.setProvCity("南京市"); 

        Province province2 = new Province(); 
        province2.setName("浙江省"); 
        province2.setProvCity("杭州市"); 

        list.add(province); 
        list.add(province2); 

        country.setProvinceList(list); 

        String str = JaxbUtil.convertToXml(country); 
        System.out.println(str.replaceFirst(BusService.JAXB_XML_HEADER, "")); 
    } 

    @Test
    public void testHcTaskParam() {
    HcTaskParam vo = new HcTaskParam();
    vo.setJssj("2017-05-05 00:00:00");

    vo.setKssj("2017-05-01 00:00:00");

    System.out.println(JaxbUtil.convertToXml(vo));
    }

    /**
     * 
     */ 
    @Test 
    public void showUnMarshaller() { 
        String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+ 
            "<country>"+ 
            "    <country_name>中国</country_name>"+ 
            "    <provinces>"+ 
            "        <province>"+ 
            "            <province_name>江苏省</province_name>"+ 
             "           <prov_city>南京市</prov_city>"+ 
            "        </province>"+ 
             "       <province>"+ 
             "           <province_name>浙江省</province_name>"+ 
             "           <prov_city>杭州市</prov_city>"+ 
             "       </province>"+ 
            "    </provinces>"+ 
            "</country>"; 
        Country country = JaxbUtil.converyToJavaBean(str, Country.class); 
        System.out.println(country); 
    } 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值